github twitter email rss
.Net Equals
0001 Jun 1
3 minutes read

.Net Equals

  • Equals must be reflexive; that is, x.Equals(x) must return true.
  • Equals must be symmetric; that is, x.Equals(y) must return the same value as y.Equals(x).
  • Equals must be transitive; that is, if x.Equals(y) returns true and y.Equals(z) returns true, then x.Equals(z) must also return true.
  • Equals must be consistent. Provided that there are no changes in the two values being compared, Equals should consistently return true or false
  • Have the type implement the System.IEquatable interface’s Equals method This generic interface allows you to define a type-safe Equals method. Usually, you’ll implement the Equals method that takes an Object parameter to internally call the type-safe Equals method.
  • Overload the == and != operator methods Usually, you’ll implement these operator methods to internally call the type-safe Equals method.

If you define a type and override the Equals method, you should also override the GetHashCode method.

The reason a type that defines Equals must also define GetHashCode is that the implementation
of the System.Collections.Hashtable type, the System.Collections.Generic.Dictionary
type, and some other collections require that any two objects that are equal must have the same
hash code value

  • Use an algorithm that gives a good random distribution for the best performance of the hash table.
  • Your algorithm can also call the base type’s GetHashCode method, including its return value. However, you don’t generally want to call Object’s or ValueType’s GetHashCode method, because the implementation in either method doesn’t lend itself to high-performance hashing algorithms.
  • Your algorithm should use at least one instance field.
  • Ideally, the fields you use in your algorithm should be immutable; that is, the fields should be initialized when the object is constructed, and they should never again change during the object’s lifetime.
  • Your algorithm should execute as quickly as possible.
  • Objects with the same value should return the same code. For example, two String objects with the same text should return the same hash code value.

	public class Object {  
	   public virtual Boolean Equals(Object obj) {  
	      // The given object to compare to can't be null  
	      if (obj == null) return false;
	     // If objects are different types, they can't be equal.  
	      if (this.GetType() != obj.GetType()) return false;  
	 
	      // If objects are same type, return true if all of their fields match  
	      // Because System.Object defines no fields, the fields match  
	      return true;  
	   }  
	}

You should always call ReferenceEquals if you want to check for identity (if two references point to the same object). You shouldn’t use the C# == operator (unless you cast both operands to Object first) because one of the operands’ types could overload the == operator, giving it semantics other than identity

public class Object {  
   public static Boolean ReferenceEquals(Object objA, Object objB) {  
      return (objA == objB);  
   }  
}

Value types

Internally, ValueType’s Equals method uses reflection. you should override Equals and provide your own implementation to improve the performance of value equality comparisons that use instances of your type.
Do not call base.Equals.

Sorting

If instances of your type will be compared for the purposes of sorting implement System.IComparable’s CompareTo method and System.IComparable’s type-safe CompareTo method. If you implement these methods, you’ll also want to overload the various comparison operator methods (<, <=, >, >=) and implement these methods internally to call the type-safe CompareTo method.


Back to posts


comments powered by Disqus