Type equality and Identity in C # (Equality & Identity)

Source: Internet
Author: User
Tags bool implement reference reflection

The CLR provides the ability to differentiate between types of equality and identity.

L Equality: If two objects are of the same type, and they each have the same and equivalent attributes. (They are instances of the same type and if each of the fields in one object matches the values of the "fields in" Object
Equality must meet three necessary conditions: reflexive, symmetrics, and transitive
Reflexive: itself equal, and a==a is always set up;
Symmetrics: Object sex, and a==b set up so b==a also set up;
Transitive: transitivity, and A==b, B==c established so A==c also set up.

L Identity: Two objects must be equal (meaning that they share the same block of memory) (the two objects have the same Values.–two objects are identical if they share an ad Dress in memory)

The CLR provides at least four ways to judge the equivalence of two objects:

1. public static bool ReferenceEquals (object left, object right);

2. public static Boolean Equals (object left, object right);

3. Public virtual Boolean Equals (object right);

4. public static bool operator== (MyClass left, MyClass right);

The ReferenceEquals method is always used to determine the identity of two objects, whether for a value type or a reference type. Therefore, for a value type, calling the method always returns false because the value type is boxed as a parameter to this method.

The static Equals method provides the ability to judge the equality of two objects and, within its implementation, invokes the third virtual Equals method above. Like ReferenceEquals, they already have the ability to judge two of objects from the bottom, and we never overwrite both methods.

The instance Equals method is also used to differentiate the equality of two objects.

L It is almost the same as the ReferenceEquals method for a reference type object. (because the equality of two reference types is often distinguishable from identity)

L and Value-type objects, we have to judge not only that they have the same object type, but also that their values are equal. Value types inherit from System.ValueType, valuetype have overridden the Object.Equals () method that could have been used to meet these requirements. However, the Valuetype.equals () method is not very effective because it has to perform a comparison of the values of the member variables they contain by reflection, without knowing the specific derived type. Therefore, it is recommended that we override the Valuetype.equals () method when we implement a data structure of a value type.

L However, we look back at the reference type, where sometimes two objects of reference type are used to compare similar value types, such as String type, which is a reference type, but it also overrides the Equals method because we use it to determine whether two strings are the same (equality). It's actually about wanting to determine whether they have the same content, which is a value semantics. Therefore, we recommend that you also override the Object.Equals () method of the base class when considering implementing a reference type that is used as a value semantic environment.

Note: Please refer to MDSN or other related documentation to see how to implement the Equals method override.

The above illustration gives a good example of how the Equals and ReferenceEquals methods are used to distinguish between equility and identity judgments.

The = = operator is an operator that can be overloaded by a class and is used to determine identity.
For reference types that do not overload = =, compare whether two reference types refer to the same object. This is the same as the Equals () method of the reference type.

For value types that do not overload = =, the operator compares whether the two values are "bitwise" equal, that is, whether each field in the two values is equal. As with the Equals method, it is recommended that you also overload the = = operator in a custom value type, because there is also the effect of reflection on efficiency.

The difference between the = = operator and the Equals method is in the polymorphic representation. The Equals method is overridden, and the = = operator is overloaded. This means that unless the compiler knows to invoke a specific overloaded version, it simply invokes the not-overloaded version of = =.


Resources:
"Essential. NET, Volume 1:the Common Language" by Donbox, Chris sells
"Applied Microsoft. NET Framework Programming" by Jeffrey Richter



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.