Open In App

C# | How to get the HashCode of the tuple?

Last Updated : 30 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

A tuple is a data structure which gives you the easiest way to represent a data set. You can also get the hash code of the tuple by using the GetHashCode Method. This method will return the hash code of the given tuple object.

Syntax:

public override int GetHashCode ();

Return Type: The return type of this method is System.Int32. It will return 32-bit signed integer hash code.

Example 1:




// C# program to illustrate the 
// use of GetHashCode method
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // creating different tuples using Create Method
        var tu1 = Tuple.Create(23, 45, 78, 89, 56);
        var tu2 = Tuple.Create(34, 45);
        var tu3 = Tuple.Create(45, 454, 454, 545, 4, 544, 54, 56);
        var tu4 = Tuple.Create(44, 58, 66, 32);
  
        // Get the hash code of the Tuples
        // Using GetHashCode method
        Console.WriteLine("HashCode for tu1: " + tu1.GetHashCode());
        Console.WriteLine("HashCode for tu2: " + tu2.GetHashCode());
        Console.WriteLine("HashCode for tu3: " + tu3.GetHashCode());
        Console.WriteLine("HashCode for tu4: " + tu4.GetHashCode());
    }
}


Output:

HashCode for tu1: 712149
HashCode for tu2: 1103
HashCode for tu3: 1582758
HashCode for tu4: 45300

Example 2:




// C# program to illustrate the use 
// of the GetHashCode method
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating different Tuples
        // using Create Method
        var t1 = Tuple.Create(64, 76, 78, Tuple.Create(12, 34, 56, 78));
  
        var t2 = Tuple.Create(78, 34, 86, Tuple.Create(23, 56));
  
        var t3 = Tuple.Create(34, 78, Tuple.Create(12, 34, 56, 78));
  
        var t4 = Tuple.Create(12, 34, 56, 34, 56, 65, 78,
                   Tuple.Create(24, 45, 67, 78, 89, 88));
  
        // Get the hash code of the Tuples
        // Using GetHashCode method
        Console.WriteLine("HashCode for t1: " + t1.GetHashCode());
        Console.WriteLine("HashCode for t2: " + t2.GetHashCode());
        Console.WriteLine("HashCode for t3: " + t3.GetHashCode());
        Console.WriteLine("HashCode for t4: " + t4.GetHashCode());
    }
}


Output:

HashCode for t1: 78746
HashCode for t2: 83573
HashCode for t3: 47540
HashCode for t4: 672122


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads