For example if we have a Dictionary defined as:
class MyClass {
public MyClass(int id) {
this.Id = id
}
public int Id { get; set; }
}
Dictionary<MyClass, int> dict = new Dictionary<MyClass, int>();
dict.Add(new MyClass(1), 1);
dict.Add(new MyClass(2), 2);
dict.Add(new MyClass(3), 3);
To ensure that the Keys are always unique, we need to implement the GetHashCode() method for the MyClass which by default extends the System.Object class. This is the method that the Dictionary internally uses to compute HashKey for the Keys.