C# is a heavy and powerful object-oriented programming language. Hence, it comes with a load of features and tools to work with OOP features such as classes, objects, properties, methods, etc.
Every C# object generates a unique integer value called a hash code. The hash code value allows us to efficiently identify the objects.
In this tutorial, we will cover the fundamentals of working with the hash code in C# objects. We will mainly focus on the GetHashCode() method which allows us to retrieve the object’s hash code.
NOTE: There is no guarantee that the hash code of objects will be entirely unique. However, they can spread across integers to avoid collisions and maximize its performance.
The Basics
You may wonder where you would need to get the hash code of the object using the GetHashCode method. In most cases, you will hardly use this method to manually grab the object’s hash code.
The main use of this method is to allow the data structures that rely on hashing such as Hash tables, dictionaries, sets, etc. to quickly locate the object in the memory. This makes the processes such as insert, delete, and search incredibly fast.
Example 1: Basic Usage
Let us look at an example usage of the GetHashCode() method when working with C# objects. Consider the following code:
{
static void Main(string[] args)
{
string str1 = "str1";
string str2 = "str2";
Console.WriteLine($"str1 hash: {str1.GetHashCode()}");
Console.WriteLine($"str2 hash: {str2.GetHashCode()}");
}
}
In the given example, we have two different string objects. We then use the GetHashCode() method to retrieve the hash codes of the two methods and print them to the console.
Output:
str2 hash: -1147906895
NOTE: Running the code twice can result in different hash codes.
Example 2: Identifying the Objects
We can also use the GetHashCode method to identify the objects in a hash set as demonstrated in the following example:
{
public string Model { get; set; }
public int Mileage { get; set; }
}
class Program
{
static void Main(string[] args)
{
HashSet<Car> cars = new HashSet<Car>();
Car car1 = new Car { Model = "Benz", Mileage = 3000 };
Car car2 = new Car { Model = "Ferrari", Mileage = 3005 };
cars.Add(car1);
cars.Add(car2);
Console.WriteLine($"car1 hash code: {car1.GetHashCode()}");
Console.WriteLine($"car2 hash code: {car2.GetHashCode()}");
}
}
In the given program, we start by defining a new “Car” class with two properties – Model and Mileage.
We then create two “Car” objects (car1 and car2) and add them to the HashSet. This allows the HashSet to use the hash codes to manage the objects.
Example 3: Overriding the Hash Codes
C# allows us to provide a custom implementation to generate the hash code of a given object. It is good to keep in mind that although this technique is allowed, it can lead to collision if used improperly. It is best to allow the compiler to handle the hash code generation instead.
using System.Xml.Linq;
class Car
{
public string Model { get; set; }
public int Mileage { get; set; }
public override int GetHashCode()
{
return Model.GetHashCode() ^ Mileage.GetHashCode();
}
}
class Program
{
static void Main(string[] args)
{
HashSet<Car> cars = new HashSet<Car>();
Car car1 = new Car { Model = "Benz", Mileage = 3000 };
Car car2 = new Car { Model = "Ferrari", Mileage = 3005 };
cars.Add(car1);
cars.Add(car2);
Console.WriteLine($"car1 hash code: {car1.GetHashCode()}");
Console.WriteLine($"car2 hash code: {car2.GetHashCode()}");
}
}
In this example, we override the GetHashCode method in the “Car” class to provide a custom implementation for the hash code generation. In this case, it is a combination of the hash code of the “Model” and “Mileage” properties.
Conclusion
In this tutorial, you understand the role and functionality of the hash code in C# objects and how it works. You also learned how to use the GetHashCode method to fetch the hash code or create a custom hash code generation.