C#中的结构与类
C# Corner今天发表了Bechir Bejaoui的一篇文章《What really make difference between structures and classes?》。关于结构和类的区别其实是老生常谈,不过本文总结得较为全面,所以翻译过来给大家共享。
总结起来,两者共有如下区别:
1、结构是值类型,类则是引用类型。因此前者是放在栈(Stack)里,后者则仅仅是将引用地址存放在栈里,而具体的值则存放在堆(heap)里。如下图所示:

2、据第1点可以得出结论,那就是类对象通常用来传递大数据,而结构对象则用来传递小数据。
3、类可以被继承,而结构则不支持。
4、结构对象不能像类对象一样赋值为null。
5、结构不能像类一样定义析构器。
6、结构不能像类一样定义为抽象的。
7、在结构中不能重写方法,除非是object类型的如下方法:
Equals()
GetHashCode()
GetType()
ToString()
若要让结构具有多态特性,可以让其实现接口。
8、在类中定义的事件是线程安全的,而结构则不是。
9、结构总是具有一个默认的公共无参构造函数,但却不能像类一样定义私有的无参构造函数(结构也不能再定义公共的无参构造函数,这与类不相同):
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
struct Me{ private Me() // compile-time error { }} class Me{ private Me() // runs Ok{} |
10、类中的静态构造函数会被调用,而结构却不能。因此在结构中定义的静态构造函数,虽然可以编译通过,但却没有价值:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
struct myStructure{ static myStructure() { Console.WriteLine("This is me a structure"); }}class myClass static myClass() { Console.WriteLine("This is me a class"); }}class Program{ static void Main(string[] args) { myStructure s = new myStructure();//Nothing happen myClass c = new myClass();//Will out put This is me a class Console.Read(); }} |
11、结构不能像类一样定义volatile字段。volatile字段主要用于并发,它相当于方法体的lock。
12、可以对结构类型使用sizeof,对类则不行。
13、类的字段会被自动初始化为0/false/null,而结构则不能。
14、在结构中不能直接对字段初始化,而类则可以。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
struct myStructure{ public string x = 2;//Not allowed }class myClass{ public string x = 2; //Allowed} |
15、结构和类对于System.Object.Equals()方法的体现是不相同的。例如定义这样的结构和类:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
struct StructurePerson{ public string FirstName; public string LastName;}class ClassPerson{ public string FirstName; public string LastName;} |
如果运行如下的代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
class Program{ static void Main(string[] args) { StructurePerson strX = new StructurePerson(); strX.LastName = "Bejaoui"; strX.FirstName = "Bechir"; StructurePerson strY = new StructurePerson(); strY.LastName = "Bejaoui"; strY.FirstName = "Bechir"; if (strX.Equals(strY)) { Console.WriteLine("strX = strY"); } else { Console.WriteLine("strX != strY"); }//This code displays strX = strY ClassPerson clsX = new ClassPerson(); clsX.LastName = "Bejaoui"; clsX.FirstName = "Bechir"; ClassPerson clsY = new ClassPerson(); clsY.LastName = "Bejaoui"; clsY.FirstName = "Bechir"; if (clsX.Equals(clsY)) { Console.WriteLine("clsX = clsY"); } else { Console.WriteLine("clsX != clsY"); }//This code displays clsX != clsY Console.Read(); }} |
浙公网安备 33010602011771号