Reprinted: referenceequals, =, equals differences

Source: Internet
Author: User
1. referenceequals, =, equals
Equals, =, referenceequals can be used to determine whether the two objects are equal. A) referenceequals
Referenceequals is a static object method. It is used to compare whether two referenced objects are referenced by the same object. For the value type, it always returns false. (Because objects after the box are always different, hehe) B) = is a binary operator that can be overloaded and can be used to compare whether two objects are equal.
For the built-in value type, = determines whether the values of the two objects are equal. It automatically performs necessary type conversion as needed, and returns true or false based on whether the values of the two objects are equal. For example, int A = 100;
Double B = 100; if (a = B)
Console. writeline ("Equal supports compare between different types !"); The above program will output:
Equal supports compare between different types! For user-defined value types, if the = operator is not overloaded, = is not usable. For example:
Struct userstruct1;
Userstruct1;
Userstruct1 B; if (a = B)
Console. writeline ("Can = reach this far ?") The above Code cannot be compiled. You can use the overload to enable = to act on user-defined value types. For the reference type, = the default behavior is the same as that of referenceequals. True is returned only when two objects point to the same reference. However, many classes in the. NET Framework overload =. For example, if the = of the string class is the same as the equals class, determine whether the content of the two strings is equal. Therefore, we recommend that you do not use the = operator for the reference type defined by the system in the application to avoid different running results in the program. C) equals is used as the built-in object method. Equals supports comparison between any two CTS objects.
Equals has a static method and a version that can be reloaded. The following program snippets explain the usage of the two methods, int A = 5;
Int B = 5; If (object. Equals (a, B ))
// You can also use if (A. Equals (B ))
{
Console. writeline ("A is equal to B ");
} In fact, the results of these two versions are exactly the same. If the user reloads the equals, the users call the equals after the user reloads them. The advantage of equals's static method is that you do not have to consider whether the object to be compared is null. The equals method has different definitions for the value type and reference type. For the value type, the type is the same, and the value is the same (for each struct member, it must be the same), equals returns true, otherwise, false is returned. 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. You can Overload equals as needed. For example, equals of the string class is used to determine whether the content of two strings is equal. Stringbuilder A = new stringbuilder ();
A. append ("the test ");
String S1 = A. tostring ();
String S2 = "the test a"; if (s2 = S1)
Console. writeline ("= returns true"); If (object. Equals (S2, S1 ))
{
Console. writeline ("equals returns true ");
} If (object. referenceequals (S2, S1 ))
{
Console. writeline ("referenceequals returns true ");
} This instance will output:
= Returns true
Equals returns true NOTE: For the string class, if S1 = "the test a" is directly declared, the output result will contain "referenceequals returns true ",
By default, only one copy is retained for the declared same string on the stack, so S1 and S2 will point to the same reference in C #. There are multiple comparison methods, there are referenceequal, equals, and sort sto, but there are nuances between them. Referenceequal instance class myclass {static void main (){
Object o = NULL;
Object P = NULL;
Object q = new object (); console. writeline (object. referenceequals (O, P ));
P = Q;
Console. writeline (object. referenceequals (p, q ));
Console. writeline (object. referenceequals (O, P ));
}
If the object points to a null reference, it returns true. In addition, P = Q; this value assignment statement only copies the address to P for the value type, there is no deep copy. Therefore, true is returned when comparing references. If we compare O. Equals (p), the compiler reports an error because the equals method cannot be null. Because of this, the referenceequals method is not welcomed by developers. In addition, equals is the virtual method of the instance, and referenceequals is the static method. = Of the primitive type, that is, Operator overloading, equals method determination, etc. Of course, there is also a static equals method. The only difference between the static equals method and the instance equals method is that when the instance equals compares two, if one is null or both are null, an exception is thrown, but the static equals method does not. The static equals method first checks if the two are null. If there is null, false is returned. If no null is found, then the instance equals method is called for comparison. Memberwiseclone () is translated as intelligent member replication. Intelligence treats value types and reference types differently. The memberwiseclone method creates a superficial copy by creating a new object and then copying the non-static fields of the current object to the new object. If the field is of the value type, perform a step-by-step copy on the field. If the field is of reference type, the referenced object is copied but not referenced. Therefore, the original object and its counterparts reference the same object. For example, an object named x references objects A and B. Object B References Object C. The child copy of X creates a new object X2, which also references objects A and B. In contrast, the deep copy of X creates a new object X2, which references the new objects A2 and B2, which are copies of A and B, respectively. B2 references the new object C2, which is a copy of C. For deep copy and shallow copy, deep copy is to create an object with the same touch. The shallow copy is still the same object, but there are two counters, the two strong references point to it.

From 51cto. com blog

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.