Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the HashCode of the tuple in C#?
To get the HashCode of a tuple in C#, you can use the GetHashCode() method. This method returns an integer hash code that represents the tuple's value. Tuples with identical values will have the same hash code, while tuples with different values will typically have different hash codes.
Syntax
Following is the syntax for getting the hash code of a tuple −
int hashCode = tuple.GetHashCode();
Return Value
The GetHashCode() method returns a 32-bit signed integer that serves as a hash code for the tuple. Equal tuples will always return the same hash code value.
Using GetHashCode() with Different Tuples
When tuples contain different values, their hash codes will be different −
using System;
public class Demo {
public static void Main(String[] args) {
var tuple1 = Tuple.Create(150, 400, 500, 700, 100, 1200, 1500, Tuple.Create(50, 100));
var tuple2 = Tuple.Create(150, 400, 500, 700, 100, 1200, 1500, Tuple.Create(100, 200));
Console.WriteLine("Is Tuple1 equal to Tuple2? = " + tuple1.Equals(tuple2));
Console.WriteLine("HashCode of Tuple1 = " + tuple1.GetHashCode());
Console.WriteLine("HashCode of Tuple2 = " + tuple2.GetHashCode());
}
}
The output of the above code is −
Is Tuple1 equal to Tuple2? = False HashCode of Tuple1 = 4621968 HashCode of Tuple2 = 4623530
Using GetHashCode() with Identical Tuples
When tuples contain identical values, their hash codes will be the same −
using System;
public class Demo {
public static void Main(String[] args) {
var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
Console.WriteLine("Is Tuple1 equal to Tuple2? = " + tuple1.Equals(tuple2));
Console.WriteLine("HashCode of Tuple1 = " + tuple1.GetHashCode());
Console.WriteLine("HashCode of Tuple2 = " + tuple2.GetHashCode());
}
}
The output of the above code is −
Is Tuple1 equal to Tuple2? = True HashCode of Tuple1 = 3231587 HashCode of Tuple2 = 3231587
Using GetHashCode() with Simple Tuples
Here's an example with simpler tuples to demonstrate the hash code behavior −
using System;
public class Demo {
public static void Main(String[] args) {
var tuple1 = Tuple.Create("Hello", 42);
var tuple2 = Tuple.Create("Hello", 42);
var tuple3 = Tuple.Create("World", 42);
Console.WriteLine("Tuple1: " + tuple1);
Console.WriteLine("HashCode of Tuple1 = " + tuple1.GetHashCode());
Console.WriteLine("Tuple2: " + tuple2);
Console.WriteLine("HashCode of Tuple2 = " + tuple2.GetHashCode());
Console.WriteLine("Tuple3: " + tuple3);
Console.WriteLine("HashCode of Tuple3 = " + tuple3.GetHashCode());
Console.WriteLine("Are Tuple1 and Tuple2 equal? " + tuple1.Equals(tuple2));
Console.WriteLine("Are Tuple1 and Tuple3 equal? " + tuple1.Equals(tuple3));
}
}
The output of the above code is −
Tuple1: (Hello, 42) HashCode of Tuple1 = -1822484662 Tuple2: (Hello, 42) HashCode of Tuple2 = -1822484662 Tuple3: (World, 42) HashCode of Tuple3 = -1805707293 Are Tuple1 and Tuple2 equal? True Are Tuple1 and Tuple3 equal? False
How It Works
The hash code of a tuple is computed based on all its elements. The GetHashCode() method combines the hash codes of individual elements using a specific algorithm to produce a single integer value. This ensures that tuples with the same content produce identical hash codes, making them suitable for use in hash-based collections like Dictionary and HashSet.
Conclusion
The GetHashCode() method provides a way to obtain a hash code for tuples in C#. Tuples with identical values will always return the same hash code, while different tuples typically produce different hash codes. This behavior makes tuples suitable for use as keys in hash-based collections.
