C# | Type.GetHashCode() Method

Type.GetHashCode() Method is used to return the hash code for this instance.

Syntax: public override int GetHashCode ();

Return Value: This method returns the hash code for the current instance.



Below programs illustrate the use of Type.GetHashCode() Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetHashCode() Method
using System;
using System.Globalization;
using System.Reflection;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object of Type
        Type objType = typeof(Dictionary<, >);
  
        // Getting hashcode of given type
        // using GetHashCode() Method
        int hash = objType.GetHashCode();
  
        // Display the Result
        Console.Write("Hashcode is {0}", hash);
    }
}

chevron_right


Output:

Hashcode is 32340152

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetHashCode() Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // calling get() method
        get(typeof(int));
        get(typeof(string));
        get(typeof(decimal));
        get(typeof(float));
    }
  
    // defining get() method
    public static void get(Type objType)
    {
  
        // Getting hashcode of given type
        // using GetHashCode() Method
        int hash = objType.GetHashCode();
  
        // Display the Result
        Console.WriteLine("Hashcode of {0} is {1}", objType, hash);
    }
}

chevron_right


Output:

Hashcode of System.Int32 is 18784216
Hashcode of System.String is 18793840
Hashcode of System.Decimal is 19137544
Hashcode of System.Single is 18792864

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.