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