对象相等性和同一性

概念同一性:同一个引用

相等性:所有部分都相等,但并一定是同一个引用。包含4个特征

1、自反性,自已等于自己

2、对称性,x=y,y也一定等于x

3、可传递性,x=y,y=z,则x=z

4、一致性,x,y都不变的情况下,比较结果不能变

Object.Equals 方法

The type of comparison between the current instance and the obj parameter depends on whether the current instance is a reference type or a value type.

比较的第一步是判断是引用类型还是值类型

1、If the current instance is a reference type, the M:System.Object.Equals(System.Object) method tests for reference equality, and a call to the M:System.Object.Equals(System.Object) method is equivalent to a call to the M:System.Object.ReferenceEquals(System.Object,System.Object) method. Reference equality means that the object variables that are compared refer to the same object.

如果是引用类型,则调用System.Object.ReferenceEquals方法

2、If the current instance is a value type, the M:System.Object.Equals(System.Object) method tests for value equality. Value equality means the following

1)The two objects are of the same type.

看是否是同一类型

2)The values of the public and private fields of the two objects are equal.

比较公有和私有字段是否相等

以上两条都满足,则相等

原文地址:https://www.cnblogs.com/qook/p/5421044.html