Understand the relationship between ReferenceEquals (), Static Equals (), instance equals (), and the operator line character = =.
When you create your own type (whether it is a class or a structure), you define the type in which case it is equal. C # provides 4 different ways to determine whether two objects are equal:
public static bool ReferenceEquals
( object left, object right );
public static bool Equals
( object left, object right );
public virtual bool Equals( object right);
public static bool operator==( MyClass left, MyClass right );
This language allows you to create your own version of all 4 of these methods. But just because can doesn ' t mean. You may never have to redefine the previous two methods. What you often encounter is the Equals () method of creating your own instance to define the semantics for your type, or you can occasionally repeat the = = operator, but only to consider the performance of the value type. Fortunately, the relationship between these 4 methods, when you change one of them, will affect the other several. Yes, 4 methods are required to complete the test to see if the object is exactly equal. But you don't have to worry, you can simply fix them.
As with most of the other complex elements in C #, this (equal comparison operation) also follows the fact that C # allows you to create both value types and reference types. Variables of two reference types are equal when referencing the same object, just as they refer to an object's ID. Variables of two value types should be equal when their type and content are the same. That's why the equivalence test has so many methods.
Let's start with two methods that you might never have modified before. Object.referenceequals () returns True when two variables are referenced to the same object, that is, two variables have the same object ID. This method always detects the object ID, not the object content, regardless of whether the type of the comparison is a reference type or a value type. Yes, that means that when you test whether two value types are equal, referenceequals () always returns false, even if you are comparing the same value type object, it returns false. Here are two boxes, which will be discussed in principle 16. Because parameters require two reference objects, calling the method with two value types will first box all two arguments, so that two referenced objects are naturally not equal. )
int i = 5;
int j = 5;
if ( Object.ReferenceEquals( i, j ))
Console.WriteLine( "Never happens." );
else
Console.WriteLine( "Always happens." );
if ( Object.ReferenceEquals( i, i ))
Console.WriteLine( "Never happens." );
else
Console.WriteLine( "Always happens." );
You may never redefine object.referenceequals () because it has actually implemented its own function: to detect the object ID of two variables (whether the same).
The second method that may never be redefined is static object.equals (). This method detects whether or not they are equal when you do not know what the two-parameter run type is. Remember: C # System.Object is the ultimate base class for all content. Any time you compare two variables, they are all instances of System.Object. So, when you don't know their type, and the equation changes by the lazy type, how does this approach compare two variables to be equal? The answer is simple: This approach gives the comparative responsibility to one of the types being compared. The static Object.Equals () method is implemented as follows:
public static bool Equals( object left, object right )
{
// Check object identity
if (left == right )
return true;
// both null references handled above
if ((left == null) || (right == null))
return false;
return left.Equals (right);
}