C#equals related

Source: Internet
Author: User

1 = = = is a value of a direct comparison value type or a reference address of a reference type, but = = cannot be used for struct,struct can only be compared with equals. = = In general, the results obtained from object.equals are equal.

2, ReferenceEquals is a static method under the object, used to compare the reference address. Therefore, the comparison of all value types is false. String is special, as long as the characters are equal, = =, equals,Object. ReferenceEquals The result of the three is true.

3, for reference types, if you need to compare, directly with equals to compare, usually return false. Therefore, you need to override them in your class.

If you are overriding equals in object, then it is best to rewrite GetHashCode, for reasons that are not fully researched and will be made up later.

You can also not override equals in object to implement equals (T obj) in iequatable<t>, which is a generic interface, so you do not have to type-check obj.

4, for arrays or tuples, you can do a structural comparison, that is, to compare arrays or tuples have the same structure and the same value, arrays and tuples have implemented IStructuralEquatable interface, the equals of this interface is explicitly implemented. Here, go directly to the code.

Code reference from: http://www.cnblogs.com/dudu/p/dotnetfx-IStructuralEquatable.html

int 1 2 3  }; int 1 2 3  //output false//output falseistructuralequatable equ =   //output True

is a built-in IEqualityComparer object that is used by default to compare structures, and there is a equals method under this interface.
Here, each element of the array1 and the corresponding element in the Array2
are compared using the equals method in the Iequalitycomparerc object, respectively, and if three comparisons return True,
Then
istructuralequatabl.equals returns True.
The comparison value type can be used Structuralcomparisons.structuralequalitycomparer, to compare reference types, you typically need to define your own IEqualityComparer


5, in the comparison of value types with Object.Equals, compare A.equals (b), if B can be implicitly converted to a, then it is visible as the same type of comparison, if not implicit conversion, it is usually returned false

C#equals related

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.