Equals
Reference Equals
是否指向类的同一个实例
(1)介绍:
ReferenceEquals is a static method that tests whether two references refer to the same instance of a class, specifically whether the two references contain the same address in memory.
(2)static静态方法不可override重载
As a static method, it cannot be overridden, so the System.Object implementation is what you always have.
(3)比较是否相等的原则
ReferenceEquals always returns true if supplied with two references that refer to the same object instance, and false otherwise. It does, however, consider null to be equal to null.
coding:
static void ReferenceEqualsSample()
{
SomeClass x = new SomeClass(), y = new SomeClass(), z = x;
bool b1 = object.ReferenceEquals(null, null);// returns true
bool b2 = object.ReferenceEquals(null, x); // returns false
bool b3 = object.ReferenceEquals(x, y); // returns false because x and y references different objects
bool b4 = object.ReferenceEquals(x, z); // returns true because x and z references the same object
}
(4)小例子
Result:false
bool b = ReferenceEquals(v,v); // v is a variable of some value type
v装在了不同的箱里
The reason is that v is boxed separately when converting each parameter, which means you get different references. Therefore, there really is no reason to call ReferenceEquals to compare value types because it doesn’t make much sense.