This article guide:
C # equals, = =, referenceequals can be used to determine whether two objects are equal to each other, and for the same basic value type, = = and equals () are the same; because ReferenceEquals () is to determine whether references to two objects are equal, for value types, because a boxing operation must be done before each decision, that is, a temporary object is generated each time, and therefore returns false forever.
One, = = operator
1, static equality symbol, corresponding to the existence of the!=, this symbol is a can be overloaded with the two-element operator can be used to compare two objects are equal.
2. It automatically makes the necessary type conversions as needed and returns TRUE or false based on whether the values of two objects are equal.
3. Compare references to referenced objects (except string reference type, string is comparison value)
4. Compare values for value types
5. Some built-in reference types overload the = = symbol, such as string to overload = = so that it compares not two string references, but compares two string literals as equals.
6, for example:
int i = 5;
int j = 5;
Console.WriteLine (i = = j);//value type comparison generation value output true
int m = 6;
Double n = 6.0;
Console.WriteLine (M = = n);//type automatically converts and compares numeric output true
object obj1 = new Object ();
Object obj2 = new Object ();
Console.WriteLine (obj2==obj1);//reference type comparison reference output false
Second, Equals
1, used to compare two objects are equal to the reference.
2, however, for value types, the same type (no automatic type conversion), and the same value (for each member of the struct must be the same), Equals returns True, or false.
3. For reference types, the default behavior is the same as referenceequals behavior, and only two objects are returned to true when they point to the same reference.
4, can be based on the need for the equals overload
5. Example
int i = 5;
int j = 5;
Console.WriteLine (I.equals (j));//value type comparison output true
int m = 6;
Double n = 6.0;
Console.WriteLine (M.equals (n)),//type does not automatically convert and compares numeric output false
Object obj1 = new Object ();
Object obj2 = new Object ();
Console.WriteLine (Obj2. Equals (OBJ1));//reference type compares output false
Console.WriteLine (obj2. Equals (String. Empty))//Output False, return false directly than the type of the Contest object
Third, ReferenceEquals
1. The static method of object, comparing the two objects ' references for equality, the value type and the reference type are the same.
2, this method cannot be overridden in an inheriting class. The prototype is: public static bool ReferenceEquals (object Obja, Object OBJB); FCL has helped us to achieve. It is the same as comparing the memory address that the reference points to.
3, for 2 value types, ReferenceEquals will always be false, because the value type after using the ReferenceEquals (object A,object B) method is unboxed to the new instance of the reference type, which naturally does not reference equality.
4, for 2 reference types, ReferenceEquals compares whether they point to the same address.
5. Example
int i = 5;
int j = 5;
Console.WriteLine (object. ReferenceEquals (i, j));/output false
int m = 6;
Double n = 6.0;
Console.WriteLine (object. ReferenceEquals (M, n));/output False
Object obj1 = new Object ();
Object obj2 = new Object ();
Console.WriteLine (object. ReferenceEquals (Obj1, OBJ2))//Output False
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.