Open In App

C# | Type.GetHashCode() Method

Last Updated : 18 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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 initializing 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);
    }
}


Output:

Hashcode is 32340152

Example 2:




// 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);
    }
}


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:



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

Similar Reads