The difference between = =, Equals, referenceequals in C #

Source: Internet
Author: User
This article is guided by:

C # equals, = =, referenceequals can be used to determine whether the individual of two objects is equal, for the same basic value type, = = and equals () Compare the result is the same; because ReferenceEquals () is to determine whether a reference to two objects is equal, for a value type, because a boxing operation must be performed before each judgment, that is, each time a temporary object is generated, and therefore always returns false.

One, = = operator

1, static equality symbol, corresponding to the existing! =, this symbol is an overloaded two-dollar operator that can be used to compare two objects for equality.

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 reference objects (except string reference types, string is comparison value)

4. Compare values for value types

5. Some built-in reference types overload the = = symbol, such as string overloading = =, so that it is not a reference to two strings, but a comparison of two string literals for equality.

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 auto-convert and compare numeric output True   object obj1 = new Object (); object obj2 = new Object (); Console.WriteLine (obj2==obj1);//reference type comparison reference output false

Second, Equals

1. The reference used to compare two objects is equal.

2, however, for value types, the same type (no type auto-conversion), and the same value (must be the same for each member of the struct), equals returns True, otherwise false.

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

4. The equals can be overloaded as needed

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 compare numeric output false  Object obj1 = new Object (); object obj2 = new Object (); Console.WriteLine (Obj2. Equals (OBJ1));//reference type compare Output Falseconsole.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, compares the two objects reference is equal, the value type and the reference type are the same.

2. You cannot override this method in an inherited class. The prototype is: public static bool ReferenceEquals (object Obja, Object OBJB); The FCL has been implemented for us. It is the same as the memory address to which the reference point is compared.

3. For 2 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, 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 difference between = =, Equals, referenceequals, hope that everyone's learning has helped, more relevant articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.