. Net equality comparison,. net equality
Reference equality and Value Equality in C #, equality is dividedReference equalityAndEquality of Values. Equality of reference refers to the equality of reference if two variables of the reference type reference the same object.
// X, y, and z all reference type variables object x = new object (); object y = new object (); object z = x; // output false, x and y do not reference the equality Console. writeLine (object. referenceEquals (x, y); // Output true, x and z have reference equality Console. writeLine (object. referenceEquals (x, z); // outputs true. x has reference equality with itself. writeLine (object. referenceEquals (x, x ));
Value Equality means that two variables-either reference type or value type-contain values with the same semantics-have equal values.
Int I = 5; int j = 6; int k = 5; // The output is false. I and j do not have the Console of equality of values. writeLine (I. equals (j); // outputs true. I and k have the Console of equality of values. writeLine (I. equals (k); // outputs true, and I has a Console of equal values. writeLine (I. equals (I ));
= And! = Operator = The operator is used to determine whether two variables are equal. The default implementation is:
- For reference types other than strings, determine whether a variable has reference equality
- For the value type, determine whether the variable has a value equality
- For the string type, determine whether the variable has equal values
! = Operator and = Operator have the opposite meaning. C # reload allowed = and! = Operator. The rule is that to reload one of them, the other must be reloaded. Object. ReferenceEquals static method Object. ReferenceEquals static method is used to judge reference equality. It accepts parameters of two object types:
Int I = 5; // The output is false. I is packed twice and is two different objects without referencing the equality Console. writeLine (object. referenceEquals (I, I); // equivalent to: object x = I; object y = I; Console. writeLine (object. referenceEquals (x, y ));
In the source code implementation of the. net Framework, the ReferenceEquals method uses the = Operator to judge:
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)][System.Runtime.Versioning.NonVersionable]public static bool ReferenceEquals (Object objA, Object objB) { return objA == objB;}
Code reference: http://referencesource.microsoft.com/#mscorlib/system/object.cs,4d607d6d56a93c7e
String equality
In. net, the string is of the reference type, but the = Operator and the Equals method both use the equality of values to compare the string. The default comparison method is case-sensitive. Sometimes we want to make a case-insensitive comparison. Therefore, the String class also has an Equals method overload that allows you to specify how to perform equality Comparison on strings:
public bool Equals(string value, StringComparison comparisonType)
The StringComparison parameter allows you to specify the region, Case, and sorting rules to be used.
The string example shows that equality is not absolute. Two identical objects may be equal in one context, but not in another context.
The Object. Equals instance method Object. Equals instance method signature is:
public virtual bool Equals(Object obj)
This is a virtual method. The specific behavior depends on whether the rewrite is performed. The Equals method in Object is implemented. If the obj parameter is the same as the current Object, true is returned.
ValueType overrides the Equals method. If the two value type variables have the same type and all instance fields are equal, they have the same value. The Equals method in ValueType implements reflection, slightly affecting the performance. For custom value types, you can consider rewriting the Equals method to avoid performance loss caused by reflection.
Object. Equals static method
This is a utility method used to determine whether two variables are equal. The method signature is:
public static bool Equals(Object objA, Object objB)
Its implementation code is as follows:
public static bool Equals(Object objA, Object objB){ if (objA == objB) { return true; } if (objA == null || objB == null) { return false; } return objA.Equals(objB);}
Analysis: 1. If two variables reference the same object or both are null, true is returned. 2. If any variable is null and the other variable is not null, false is returned. 3. If neither of the two variables is null and the same object is not referenced, The objA. Equals (objB) result is returned. Override Object. Equals instance method
When rewriting the Object. Equals instance method, follow the following rules:
- Auto-inverse: x. Equals (x) should return true
- Symmetric: x. Equals (y) and y. Equals (x) should have the same results
- Pass: If x. Equals (y) is true and y. Equals (z) is true, x. Equals (z) should be true.
- Stable: If x and y are not modified, consecutive calls to x. Equals (y) should have the same results
- When the real parameter is null, always return null.
- If you override the Equals method, you should also override the GetHashCode method, and the GetHashCode method of two equal objects should return the same value.
IEquatable <T> generic interface
IEquatable <T> defines a type-specific equality comparison method. This interface only defines one method and the signature is:
bool Equals(T other)
This interface is introduced with the generic set starting from. net 2.0. When you call methods such as Contains, IndexOf, LastIndexOf, and Remove on a generic set, this interface is used for Equality comparison. According to MSDN, any class that may be stored in a generic set should implement this interface.
If the IEquatable interface is implemented, you should also override the Object. Equals instance method and Object. GetHashCode method.
IEqualityComparer <T> generic interface and EqualityComparer <T> generic class
IEqualityComparer declares two methods:
bool Equals(T x, T y)int GetHashCode(T obj)
This interface allows you to skip the Equals and GetHashCode implementations of T in a generic set. If you provide an instance of IEqualityComparer <T> to a generic set, then the set will use the Equals and GetHashCode methods of IEqualityComparer <T> for comparison.
EqualityComparer <T> provides a base class for The Implementation of The IEqualityComparer <T> generic interface. The EqualityComparer <T>. Default attribute returns the Default equality comparator. The cooperation between IEquatable <T> and generic sets actually works here:
- If T implements the IEquatable <T> interface, EqualityComparer <T>. Default uses the IEquatable <T> interface for Equality comparison.
- Otherwise, EqualityComparer <T>. Default uses Object. Equals for Equality comparison.