c# vs c++
2014-06-30 21:45
148 查看
【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会导致编译错误
View Code
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、在 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 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 */
View Code
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
相关文章推荐
- 应用程序的性能: C# vs C/C++
- 【原】C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)
- C++ VS C#(5):数组
- C++ VS C#(7):指向函数的指针和委托
- C++ VS C#(6):入口函数,改变形参数值
- C++VS C#
- C++ VS C#(9):继承
- C++ VS C#(4):枚举,结构体
- C++ VS C#(3):switch,类型转换
- C++ VS C#(8):类定义
- C# vs C++之一:委托 vs 函数指针
- C++ VS C# Key Points
- 好文转载:C# vs C++之一:委托 vs 函数指针
- C++ VS C#(4):枚举,结构体
- C++ VS C#(2):字符串,命名空间
- C#的委托vsC++的回调函数
- C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)[转自柒笑侠]
- C++ VS C#(1):注释,变量,控制台输出
- 编写高效的C#图像处理程序(4) Rgb=>Lab,C# vs. C++(VC)
- C++ VS C#(8):类定义