c# vs c++

1、在 C++ 中,类和结构实际上是相同的,而在 C# 中,它们很不一样。C# 类可以实现任意数量的接口,但只能从一个基类继承。而且,C# Struct不支持继承,也不支持显式默认构造函数(必须提供参数化构造函数)。

  1)It is an error to define a default (parameterless) constructor for a struct. It is also an error to initialize an instance field in a struct body. You can initialize struct members only by using a parameterized constructor or by accessing the members individually after the struct is declared. Any private or otherwise inaccessible members can be initialized only in a constructor.

  2) A struct cannot inherit from another struct or class, and it cannot be the base of a class. A struct can implement interfaces, and it does that exactly as classes do.

  3)A struct is a value type, while a class is a reference type

  4)When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. In such a case, there is no constructor call, which makes the allocation more efficient. However, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.

  5)下述代码,把struct换成class会导致编译错误

public struct CoOrds
{
public int x, y; public CoOrds(int p1, int p2)
{
x = p1;
y = p2;
}
}
// Declare a struct object without "new."
class TestCoOrdsNoNew
{
static void Main()
{
// Declare an object:
CoOrds coords1; // Initialize:
coords1.x = ;
coords1.y = ; // Display results:
Console.Write("CoOrds 1: ");
Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y); // Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
// Output: CoOrds 1: x = 10, y = 20

  6)在栈上定义数组,则只能像如下这样,没有C++方便。

 public struct CoOrds
{
public int x, y;
public CoOrds(int p1, int p2)
{
x = p1;
y = p2;
}
} // Declare a struct object without "new."
class TestCoOrdsNoNew
{
static void Main()
{
// Declare an object:
CoOrds coords1; // Initialize:
coords1.x = ;
coords1.y = ; // Display results:
System.Console.Write("CoOrds 1: ");
System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
}
}

2、在 C++ 中,数组只是一个指针。在 C# 中,数组是包含方法和属性的对象。例如,可通过 Length 属性查询数组的大小。C# 数组还使用索引器(验证用于访问数组的各个索引)。声明 C# 数组的语法不同于声明 C++ 数组的语法:在 C# 中,“[]”标记出现在数组类型之后,而非变量之后。

  1)数组类型是从抽象基类型 Array 派生的引用类型。由于此类型实现了 IEnumerable 和 IEnumerable<T>,因此可以对 C# 中的所有数组使用 foreach 迭代。

  2)索引器。this 关键字用于定义索引器。索引器可被重载。value 关键字用于定义由 set 索引器分配的值。

 class SampleCollection<T>
{
private T[] arr = new T[];
public T this[int i]
{
get
{
return arr[i];
}
set
{
arr[i] = value;
}
}
} // This class shows how client code uses the indexer
class Program
{
static void Main(string[] args)
{
SampleCollection<string> stringCollection = new SampleCollection<string>();
stringCollection[] = "Hello, World";
System.Console.WriteLine(stringCollection[]);
}
}

3、在 C++ 中,bool 类型实质上是一个整数。在 C# 中,不存在 bool 类型与其他类型之间的相互转换。

  1)bool 关键字是 System.Boolean 的别名。它用于声明变量来存储布尔值 true 和 false

4、long 类型在 C# 中为 64 位,而在 C++ 中为 32 位。

5、在 C++ 中,除非显式通过指针或引用传递,否则所有变量都通过值传递。在 C# 中,除非显式通过具有 ref 或 out 参数修饰符的引用传递,否则类通过引用传递,而结构通过值传递。

6、与 C++ 中的 switch 语句不同,C# 不支持从一个 case 标签贯穿到另一个 case 标签。

7、base 关键字用于从派生类中访问基类的成员。基类访问只能在构造函数、实例方法或实例属性访问器中进行。 从静态方法中使用 base 关键字是错误的。

public class Person
{
protected string ssn = "444-55-6666";
protected string name = "John L. Malgraine"; public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
// Calling the base class GetInfo method:
base.GetInfo();
Console.WriteLine("Employee ID: {0}", id);
}
} class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
/*
Output
Name: John L. Malgraine
SSN: 444-55-6666
Employee ID: ABC567EFG
*/

8、C++ 通过继承支持方法的隐式“隐藏”。在 C# 中,必须使用 new 修饰符来显式隐藏继承的成员。

9、预处理器指令用于条件编译。C# 中不使用头文件。

10、static const不能同时使用,因为在一个类,const变量实际上就是一个static变量。

11、在 C# 中,只有在 unsafe 模式下才允许使用指针。

12、在 C++ 中,字符串只是字符的数组。在 C# 中,字符串是支持可靠搜索方法的对象。

13、在 C++ 中,多个模块公用的类型放置在头文件中。在 C# 中,可通过元数据获取此信息。

14、C# 中的局部变量在初始化前不能使用。

15、C# 不支持位域。

参考:

1、http://msdn.microsoft.com/zh-cn/library/8b0bdca4(v=vs.90).aspx

2、http://msdn.microsoft.com/zh-cn/library/6x16t2tx(v=vs.90).aspx

3、http://msdn.microsoft.com/zh-cn/library/yyaad03b(v=vs.90).aspx

随机推荐

  1. Tencent Server Web 安装试用

    Tencent Server Web 安装试用 私有环境搭建,使用docker-compose 进行memcache 安装 参考github 代码 https://github.com/rongfen ...

  2. MySQL的安装配置教程

    1. 官网下载ZIP压缩版本(本人电脑是64位的) x64bit MySQL Community 2. 解压到E:\SoftwareFiles\mysql-5.7.11-winx64 3. 在E:\S ...

  3. 使用Apache Archiva管理Maven仓库

    1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库:否则,私服请求外部 ...

  4. Go语言学习笔记 package

    加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 strings import "strings" strings包实现了用于操作字符的简单函数 ...

  5. TCP/IP概念简述

    这里所说的是广义上的TCP/IP协议群,而不是特指TCP和IP这两种具体的协议.既然是协议群,那么都有哪些协议呢?我们先不着急回答这个问题,因为要弄清楚这个问题,首先得了解另两件事,就是为啥要有这个协 ...

  6. 试玩swoole扩展 第一天

    安装 pecl install swoole 部分过程: configure: creating ./config.statusconfig.status: creating config.hrunn ...

  7. 设计WEB数据库(学习)

    设计WEB数据库 1.考虑建模的实际对象 为现实世界的实体和关系建立模型 在上面情况下考虑建表呢? 答:如果有一组属于同一类型的数据,就可以根据这些数据创建表 2.避免保存冗余数据 原因:a.空间的浪 ...

  8. dos命令行连接操作ORACLE数据库

    C:\Adminstrator> sqlplus "/as sysdba" 查看是否连接到数据库 SQL> select status from v$instance; ...

  9. 双口RAM,值得研究

    在FPGA设计过程中,使用好双口RAM,也是提高效率的一种方法. 官方将双口RAM分为简单双口RAM和真双口RAM. 简单双口RAM只有一个写端口,一个读端口. 真双口RAM分别有两个写端口和两个读端 ...

  10. SpringBoot运行报Address already in use: bind

    java.net.BindException: Address already in use: bind at sun.nio.ch.Net.bind0(Native Method) at sun.n ...