Most types in .NET derive from the type System.Object
, simply called object
in C#. (E.g. interfaces don’t, however their implementations do.)
System.Object
declares the methods Equals
and GetHashCode
as well as other members. (Note: The case matters in C#). The types you create automatically inherit these methods.
The task of Equals
is to compare an object to another. The default implementation for reference types is to compare the references. If you want to change this behavior, you will have to override this method.
GetHashCode
calculates the hash code of an object and is used in hash tables. For instance the types Dictionary<TKey,TValue>
and HashSet<T>
make use of it.
See Hashtable and Dictionary Collection Types. If you override Equals
, you have to override GetHashCode
as well in order to keep consistency.