How to get the hashcode for enum in C#?
Last Updated :
23 Jul, 2019
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:
using System;
class GFG {
enum Color {Blue, Black};
public static void Main(String[] args)
{
Color c1 = Color.Blue;
Console.Write( "HashCode of Enum Constant " + c1 + " : " );
Console.WriteLine(c1.GetHashCode());
Color c2 = Color.Black;
Console.Write( "Hashcode of Enum Constant " + c2 + " : " );
Console.WriteLine(c2.GetHashCode());
}
}
|
Output:
HashCode of Enum Constant Blue : 0
Hashcode of Enum Constant Black : 1
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...