In this post, you will see what does GetHashCode() in a class. The GetHashCode() belongs to object class which super type of all classes. So, GetHashCode() is available to all classes with default implementation. Look at the following example in this regard:
Example1class Animal
{
public string Name { get; set; } = string.Empty;
public override int GetHashCode()
{
// using object base class implementation
return base.GetHashCode();
}
}
class Program
{
static void Main()
{
Animal animal = new Animal { Name = "Cat" };
Animal animal2 = new Animal { Name = "Cat" };
Console.WriteLine(animal.GetHashCode());
Console.WriteLine(animal2.GetHashCode());
Animal animal3 = new Animal { Name = "Dog" };
animal3 = animal2;
Console.WriteLine(animal3.GetHashCode());
}
}
The Animal class uses the default implementation. The developer has not provided any custom implementation. When you run the code, you get an integer value for each instance of Animal class. Remember that this value is not identity of animal object. Different objects of a class may have same hash value. But same objects will have same hash code. So, we get same hash value for animal3 and animal2 as they point to same object.
OUTPUT
43942917
59941933
59941933
Note that animal3 and animal2 both points to same animal object, due to animal3=animal2; statement. Therefore, we get same hash code.
Points to Remember
- If two variables point to same object then their hash code will be same. However, the converse is not necessarily true.
- If two objects have same hash code then it does not mean that they are same objects. Hash code may collide for different objects. It also means that hash code are not unique to objects.
Example2
Suppose that we want to generate hash code based on properties of animal object. In this case, developer must provide his own implementation of GetHashCode method. The following example shows custom implementation of GetHashCode method:
class Animal
{
public string Name { get; set; } = string.Empty;
public override int GetHashCode()
{
// hash code based on current instance property i.e. Name
return this.Name.GetHashCode();
}
}
class Program
{
static void Main()
{
Animal animal = new Animal { Name = "Cat" };
Animal animal2 = new Animal { Name = "Cat" };
Animal animal3 = new Animal { Name = "Dog" };
Console.WriteLine(animal.GetHashCode());
Console.WriteLine(animal2.GetHashCode());
Console.WriteLine(animal3.GetHashCode());
}
}
The output is:
238193233
238193233
-374578683
Reason: animal and animal2 produce the same hash code because both contain the same Name.
However, equal hash codes do not prove objects are equal. Different data can still produce identical hash codes.
Remember: Equal hash codes do not prove objects are equal. Different data can still produce identical hash codes.
Application of GetHashCode
When Equals method is overridden in a class for checking value type equality of objects, GetHashCode is also overridden for safety measures. If Equals returns true and GetHashCode returns same value for both objects then it is safe to assume that both objects are same content wise. But remember that GetHashCode is not fullproof.
Example:class Animal
{
public string Name { get; set; } = "";
public override bool Equals(object? obj)
{
return obj is Animal a &&
Name == a.Name;
}
public override int GetHashCode()
{
return Name.GetHashCode(); // Hash code based on Name of current object
}
}
Now in the client code, for the same content objects of Animal class, Equals() returns True value and hash values are same for both objects:
class Program
{
static void Main()
{
Animal a1 = new Animal { Name = "Cat" };
Animal a2 = new Animal { Name = "Cat" };
Console.WriteLine(a1.Equals(a2)); // True
Console.WriteLine(a1.GetHashCode()); // same hash code
Console.WriteLine(a2.GetHashCode()); // same hash code
}
}