a.Equal(b)
code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6
7namespace Identity
8{
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 MyClass my1 = new MyClass(1);
14 MyClass my2 = new MyClass(1);
15 Assert.IsFalse(my1.Equals(my2));//引用类型:没有指向同一内存地址
16
17 int i1 = 1;
18 int i2 = 1;
19 Assert.IsTrue(i1.Equals(i2));//值类型:通过反射
20
21 MyStruct myStruct1 = new MyStruct(1,"1");
22 MyStruct myStruct2 = new MyStruct(1,"1");
23 Assert.IsTrue(myStruct1.Equals(myStruct2));//值类型:通过反射,比每个属性值。
24
25 string s1 = "1";
26 string s2 = "1";
27 Assert.IsTrue(s1.Equals(s2));//String为引用类型,但其Equal方法并重写,行为与值类型一样。
28
29 MyCopStruct mc1 = new MyCopStruct(new MyClass(1));
30 MyCopStruct mc2 = new MyCopStruct(new MyClass(1));
31 Assert.IsFalse(mc1.Equals(mc2));//值类型:通过反射,比每个属性值。属性为引用类型,是不同实例。
32
33 MyCopStruct mc11 = new MyCopStruct(my1);
34 MyCopStruct mc12 = new MyCopStruct(my1);
35 Assert.IsTrue(mc11.Equals(mc12));/**/////值类型:通过反射,比每个属性值。属性为引用类型,是相同实例。
36
37 MyCopStruct1 mc111 = new MyCopStruct1(mc11);
38 MyCopStruct1 mc112 = new MyCopStruct1(mc12);
39 Assert.IsTrue(mc111.Equals(mc112));/**/////值类型:通过反射,比每个属性值。属性为值类型,且值相同。
40
41 Console.WriteLine("OK");
42 Console.ReadKey();
43 }
44 }
45
46 public class MyClass
47 {
48 public int name;
49
50 public MyClass(int name)
51 {
52 this.name = name;
53 }
54 }
55
56 public struct MyStruct
57 {
58 public int name;
59 public string strName;
60
61 public MyStruct(int name,string strName)
62 {
63 this.name = name;
64 this.strName = strName;
65 }
66 }
67
68 public struct MyCopStruct
69 {
70 public MyClass myclass;
71
72 public MyCopStruct(MyClass myclass)
73 {
74 this.myclass = myclass;
75 }
76 }
77
78 public struct MyCopStruct1
79 {
80 public MyCopStruct myclass;
81
82 public MyCopStruct1(MyCopStruct myclass)
83 {
84 this.myclass = myclass;
85 }
86 }
87
88 public class MyClass2
89 {
90 public override bool Equals(object obj)//需要同时重写GetHashCode,不知道为啥。
91 {
92 return base.Equals(obj);
93 }
94 public override int GetHashCode()
95 {
96 return base.GetHashCode();
97 }
98 }
99}
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6
7namespace Identity
8{
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 MyClass my1 = new MyClass(1);
14 MyClass my2 = new MyClass(1);
15 Assert.IsFalse(my1.Equals(my2));//引用类型:没有指向同一内存地址
16
17 int i1 = 1;
18 int i2 = 1;
19 Assert.IsTrue(i1.Equals(i2));//值类型:通过反射
20
21 MyStruct myStruct1 = new MyStruct(1,"1");
22 MyStruct myStruct2 = new MyStruct(1,"1");
23 Assert.IsTrue(myStruct1.Equals(myStruct2));//值类型:通过反射,比每个属性值。
24
25 string s1 = "1";
26 string s2 = "1";
27 Assert.IsTrue(s1.Equals(s2));//String为引用类型,但其Equal方法并重写,行为与值类型一样。
28
29 MyCopStruct mc1 = new MyCopStruct(new MyClass(1));
30 MyCopStruct mc2 = new MyCopStruct(new MyClass(1));
31 Assert.IsFalse(mc1.Equals(mc2));//值类型:通过反射,比每个属性值。属性为引用类型,是不同实例。
32
33 MyCopStruct mc11 = new MyCopStruct(my1);
34 MyCopStruct mc12 = new MyCopStruct(my1);
35 Assert.IsTrue(mc11.Equals(mc12));/**/////值类型:通过反射,比每个属性值。属性为引用类型,是相同实例。
36
37 MyCopStruct1 mc111 = new MyCopStruct1(mc11);
38 MyCopStruct1 mc112 = new MyCopStruct1(mc12);
39 Assert.IsTrue(mc111.Equals(mc112));/**/////值类型:通过反射,比每个属性值。属性为值类型,且值相同。
40
41 Console.WriteLine("OK");
42 Console.ReadKey();
43 }
44 }
45
46 public class MyClass
47 {
48 public int name;
49
50 public MyClass(int name)
51 {
52 this.name = name;
53 }
54 }
55
56 public struct MyStruct
57 {
58 public int name;
59 public string strName;
60
61 public MyStruct(int name,string strName)
62 {
63 this.name = name;
64 this.strName = strName;
65 }
66 }
67
68 public struct MyCopStruct
69 {
70 public MyClass myclass;
71
72 public MyCopStruct(MyClass myclass)
73 {
74 this.myclass = myclass;
75 }
76 }
77
78 public struct MyCopStruct1
79 {
80 public MyCopStruct myclass;
81
82 public MyCopStruct1(MyCopStruct myclass)
83 {
84 this.myclass = myclass;
85 }
86 }
87
88 public class MyClass2
89 {
90 public override bool Equals(object obj)//需要同时重写GetHashCode,不知道为啥。
91 {
92 return base.Equals(obj);
93 }
94 public override int GetHashCode()
95 {
96 return base.GetHashCode();
97 }
98 }
99}