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:
// 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()); } } |
Output:
HashCode of Enum Constant Blue : 0 Hashcode of Enum Constant Black : 1
Reference:
- https://docs.microsoft.com/en-us/dotnet/api/system.enum.gethashcode?view=netframework-4.8
Contact Us