How to get the hashcode for enum in C#?

Enum.GetHashCode Method is used to get the HashCode for the value of the current instance. This method is inherited from the Object class.

Syntax:

public override int GetHashCode ();

Returns: This method returns the 32-bit signed integer hash code.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Enum.GetHashCode() Method
using System;
  
class GFG {
      
    enum Color {Blue, Black};
  
    // Main Method
    public static void Main(String[] args)
    {
        Color c1 = Color.Blue;
        Console.Write("HashCode of Enum Constant " + c1 + " : ");
  
        // Using the GetHashCode() Method
        Console.WriteLine(c1.GetHashCode());
  
        Color c2 = Color.Black;
        Console.Write("Hashcode of Enum Constant " + c2 + " : ");
  
        // Using the GetHashCode Method
        Console.WriteLine(c2.GetHashCode());
    }
}

chevron_right


Output:

HashCode of Enum Constant Blue : 0
Hashcode of Enum Constant Black : 1

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.