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. elixir mix开发入门

    备注: 简单使用mix 进行项目的生成,同时添加docker 构建支持 1. 生成项目 mix new mydemoproject 输出信息如下: * creating README.md * cre ...

  2. jQuery的deferred对象详解(转载)

    jQuery的开发速度很快,几乎每半年一个大版本,每两个月一个小版本.(由于无法转载,复制原文 .原文链接——原作者:阮一峰) 每个版本都会引入一些新功能.今天我想介绍的,就是从jQuery 1.5. ...

  3. laravel的学习历程

    首要,表明态度:PHP是世界上最佳的言语.(梗) laravel说是php将来,形似不假. 最开端触摸的是thinkphp,格外喜爱她的分层,文档格外完全,阅读起来没任何妨碍. 比较laravel,我 ...

  4. laravel的auth用户认证的例子

    参考http://www.cnblogs.com/yjf512/p/4042356.html 需要注意的是,生成的测试数据,password部分必须用laravel自带的hash一下 Hash::ma ...

  5. HDOJ5883(欧拉路)

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  6. 【转】Jmeter入门:如何建立和使用Jmeter测试环境

    一.工具描述 apache jmeter是100%的java桌面应用程序,它被设计用来加载被测试软件功能特性.度量被测试软件的性能.设计jmeter的初衷是测试web应用, 后来又扩充了其它的功能.j ...

  7. Git回版本回退

    这里我们使用命令行的方式对已经提交的版本进行强行回退操作~~~ 一.将git的安装目录bin放到path路径中, 如下图所示: 二.进入cmd界面,依次输入下面内容即可(git 远程仓库 回退到指定版 ...

  8. 网络 、osi 七层模型、tcp/ip 五层参考

    网络 网络的本质就是通讯,比特传输 网络拓扑 物理布局pc -- 交换机 -- 路由器逻辑布局pc -- 路由器 交换机的产生 网络之初,是通过网线互相连通到各个主机,存在的问题就是2个pc都要与服务 ...

  9. MySql命令集合

    1.mysql命令用户连接数据库 mysql命令格式: mysql -h主机地址 -u用户名 -p用户密码 (1) 连接到本机上的MYSQL 首先打开DOS窗口,然后进入目录mysql\bin,再键入 ...

  10. JavaScript中的跨域详解(二)

    4.AJAX 同源政策规定,AJAX请求只能发给同源的网址,否则就报错. 除了架设服务器代理(浏览器请求同源服务器,再由后者请求外部服务),有三种方法规避这个限制. JSONP WebSocket C ...