. NET

Source: Internet
Author: User

Method  

Category 

Class default behavior 

NativeClassType 

EndStructure 

DifferentType SimilarityAnd so on 

LineIs it possible to overrideWrite

Application fieldJing

Object. Equals

Static Method

Call the Equals instance method

Equal Value

Field Values are equal

Yes

Yes

The parameter may be Null.

Object. ReferenceEquals

Static Method

References are equal

Returns False.

Returns False.

Not Allowed

Not Allowed

Test

=

Operator

References are equal

Equal Value

Must be defined before use

Not Allowed

Yes

Native type, simple business logic

Equals

Instance method

References are equal

The value and type are equal.

Field Values are equal

Yes

Yes

General Processing

For the value type, the implementation = operation is like an embellishment. If you want to implement equal comparison operations, you should first override the Equals method (similarly, If You Want To implement size comparison, you should first implement
IComparable interface instead of comparison operator). When the Equals method inherited from the Object is used for value type, all fields of the two objects are compared.
True. It is said that reflection is used, and the efficiency is very low. However, if it is low, why must it be overwritten first?

Because all. Net
The Framework Key-value set is compared using the Equals instance method, so it is actually a legal balance in. Net, whether it is a native type, structure or class instance, it should be
The Equals method serves as its standard equal comparison method, including the types we implement ourselves. The benefits of using the instance method can also be understood, more flexible, we can add some overloaded Equals method
Different prerequisites for comparison. Works with the default rewritten Equals method to form a complete set of comparison rules to meet the complex and variable standards in reality.

When rewriting the Equals method, the official recommendation is to rewrite the GetHashCode method. If you don't use this type as the key-value set key, it doesn't matter.

In some cases, when the complexity is to overload the Equals method, we need to define a function class dedicated to comparing equal .. Net
The Framework already provides an interface System. Collections. IEqualityComparer and has several built-in implementations, such
For StringComparer and EqualityComparer, the comparison class we write may also implement this interface. Of course, as long as it can be used, you don't have to worry about that much. View. Net
Framework source code, you can find that many messy classes are used to compare equal, probably internal privileges.

Part of the full text of the link: http://www.cnblogs.com/XmNotes/archive/2011/06/23/2088341.html

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.