equal 与 ==
var s1 = new { Name = "abc" };
var s2 = new { Name = "abc" };
Debug.WriteLine(s1.Equals(s2));
Debug.WriteLine(s1 == s2);
var s3 = s2;
Debug.WriteLine(s2.Equals(s3));
Debug.WriteLine(s2 == s3);
.entrypoint
// 代码大小 92 (0x5c)
.maxstack 2
.locals init ([0] class '<>f__AnonymousType0`1'<string> s1,
[1] class '<>f__AnonymousType0`1'<string> s2,
[2] class '<>f__AnonymousType0`1'<string> s3)
IL_0000: nop
IL_0001: ldstr "abc"
IL_0006: newobj instance void class '<>f__AnonymousType0`1'<string>::.ctor(!0)
IL_000b: stloc.0
IL_000c: ldstr "abc"
IL_0011: newobj instance void class '<>f__AnonymousType0`1'<string>::.ctor(!0)
IL_0016: stloc.1
IL_0017: ldloc.0
IL_0018: ldloc.1
IL_0019: callvirt instance bool [mscorlib]System.Object::Equals(object)
IL_001e: box [mscorlib]System.Boolean
IL_0023: call void [System]System.Diagnostics.Debug::WriteLine(object)
IL_0028: nop
IL_0029: ldloc.0
IL_002a: ldloc.1
IL_002b: ceq
IL_002d: box [mscorlib]System.Boolean
IL_0032: call void [System]System.Diagnostics.Debug::WriteLine(object)
IL_0037: nop
IL_0038: ldloc.1
IL_0039: stloc.2
IL_003a: ldloc.1
IL_003b: ldloc.2
IL_003c: callvirt instance bool [mscorlib]System.Object::Equals(object)
IL_0041: box [mscorlib]System.Boolean
IL_0046: call void [System]System.Diagnostics.Debug::WriteLine(object)
IL_004b: nop
IL_004c: ldloc.1
IL_004d: ldloc.2
IL_004e: ceq
IL_0050: box [mscorlib]System.Boolean
IL_0055: call void [System]System.Diagnostics.Debug::WriteLine(object)
IL_005a: nop
IL_005b: ret
结果是……

浙公网安备 33010602011771号