Differences between =, Equals, and ReferenceEquals in C,

Source: Internet
Author: User

Differences between =, Equals, and ReferenceEquals in C,

Link: http://www.studyofnet.com/news/1188.html

Introduction:

In C #, Equals, =, ReferenceEquals can be used to determine whether the two objects are equal. For the same basic value type, the comparison result of = and Equals () is the same; because ReferenceEquals () is used to determine whether the references of two objects are equal, for the value type, because the packing operation must be performed before each judgment, that is, a temporary object is generated each time, therefore, false is always returned.

I. = Operator

1. Static equal symbols, corresponding to existing! =, This symbol is a binary operator that can be overloaded. It can be used to compare whether two objects are equal.

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

3. Compare the reference object (except for the string reference type, the string is the comparison value)

4. Compare the value of the Value Type

5. Some built-in reference types are overloaded with the = symbol. For example, if the string is reloaded with =, it compares the two strings instead of the references of the two strings, but whether the two strings are equal.

6. For example:

1 int I = 5; 2 int j = 5; 3 Console. writeLine (I = j); // value type comparison generation value output True4 5 int m = 6; 6 double n = 6.0; 7 Console. writeLine (m = n); // the data type is automatically converted and the value output is True.
1 object obj1 = new object (); 2 object obj2 = new object (); 3 Console. WriteLine (obj2 = obj1); // compare the reference type with the reference type and output False

Ii. Equals

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

2. However, if the value type is the same (the type is not automatically converted) and the value is the same (each struct member must be the same), Equals returns true; otherwise, false.

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

4. You can reload Equals as needed.

5. instance:

1 int I = 5; 2 int j = 5; 3 Console. writeLine (I. equals (j); // value type comparison outputs True 4 5 int m = 6; 6 double n = 6.0; 7 Console. writeLine (m. equals (n); // The type is not automatically converted and the comparison value is output to False 8 9 object obj = new object (); 10 object obj1 = new object (); 11 object obj2 = new object (); 12 Console. writeLine (obj2.Equals (obj1); // compare the reference type and output False13 Console. writeLine (obj1.Equals (obj); // compare the reference type and output False14 Console. writeLine (obj2.Equals (string. empty); // outputs False. If the two objects are of different types, False15 obj1 = obj; 16 obj2 = obj; 17 Console is returned. writeLine (obj2.Equals (obj1); // compare the reference type and output True18 Console. writeLine (obj1.Equals (obj); // compare the reference type and Output True

Iii. ReferenceEquals

1. Compare the static method of the Object to see if the references of the two objects are equal. Both the value type and the reference type are the same.

2. You cannot override this method in the inheritance class. The prototype is public static bool ReferenceEquals (object objA, object objB). FCL has helped us implement it. It compares whether the referenced memory address is the same.

3. For two value types, ReferenceEquals will always be false, because the value type is reboxed into a new reference type instance after the ReferenceEquals (object a, object B) method is used, naturally, the references are not equal.

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

5. instance:

1 int I = 5; 2 int j = 5; 3 Console. writeLine (object. referenceEquals (I, j); // output False 4 5 int m = 6; 6 double n = 6.0; 7 Console. writeLine (object. referenceEquals (m, n); // output False 8 9 object obj = new object (); 10 object obj1 = new object (); 11 object obj2 = new object (); 12 Console. writeLine (object. referenceEquals (obj1, obj2); // outputs False13 Console. writeLine (object. referenceEquals (obj1, obj); // output False14 obj1 = obj; 15 obj2 = obj; 16 Console. writeLine (object. referenceEquals (obj1, obj2); // output True17 Console. writeLine (object. referenceEquals (obj1, obj); // outputs True

 

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.