Explain the difference between = = Equals and ReferenceEquals in C in detail

  • 2021-11-13 02:31:09
  • OfStack

Introduction to this article:

Equals, = = and ReferenceEquals in C # can all be used to judge whether the individuals of two objects are equal or not. For the same basic value type, the comparison result between = = and Equals () is one; Because ReferenceEquals () determines whether the references of two objects are equal, for value types, false is always returned because boxing operation must be performed before each judgment, that is, a temporary object is generated every time.

1. = = Operator

1. Static equality symbol corresponds to existence! =, which is a 2-yuan operator that can be overloaded and can be used to compare whether two objects are equal.

2. It automatically performs the necessary type conversion as needed, and returns true or false based on whether the values of the two objects are equal.

3. Compare references to reference objects (except for reference types string, where string is the comparison value)

4. Compare values for value types

5. Some built-in reference types overload the = = symbol. For example, string overloads = =, so that it compares not the references of two strings, but whether the literal quantities of the two strings are equal.

6. For example:


int i = 5;
int j = 5;
Console.WriteLine(i == j);// Value type comparison algebra   Output True

int m = 6;
double n = 6.0;
Console.WriteLine(m == n);// Automatic conversion of types and comparison of numeric values   Output True 

object obj1 = new object();
object obj2 = new object();
Console.WriteLine(obj2==obj1);// Reference type comparison reference   Output False

2. Equals

1, used to compare whether the references of two objects are equal.

2. For value types, however, Equals returns true if the types are the same (no automatic type conversion is performed) and the values are the same (each member of struct must be the same), otherwise false.

3. For reference types, the default behavior is the same as that of ReferenceEquals, and true is returned only when two objects point to the same Reference.

4. You can overload Equals as needed

5. Examples


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));// Types do not automatically convert and compare numeric values   Output False

object obj1 = new object();
object obj2 = new object();
Console.WriteLine(obj2.Equals(obj1));// Reference type comparison   Output False
Console.WriteLine(obj2.Equals(string.Empty));// Output False, Returns directly if the type of the comparator object is different False  

3. ReferenceEquals

1. The static method of Object compares whether the references of two objects are equal, and both the value type and the reference type are 1.

2. You cannot override this method in an inherited class. The prototype is: public static bool ReferenceEquals (object objA, object objB); FCL has helped us achieve it. It is to compare whether the memory address pointed by the reference is 1.

3. For two value types, ReferenceEquals is always false, because the value type is re-boxed as a new instance of the reference type after using the ReferenceEquals (object a, object b) method, and naturally there is no reference equality.

4. For two reference types, ReferenceEquals compares whether they point to the same 1 address.

5. Examples


     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

Related articles: