【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 = 10; coords1.y = 20; // 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