C# program to get the hash-code of enum constants
//C# program to get the hash-code of enum constants.
using System;
enum COLOR
{
RED, GREEN, YELLOW, BLACK, WHITE, BLUE
}
class EnumDemo
{
static void PrintHashCode(COLOR color)
{
switch (color)
{
case COLOR.RED:
Console.WriteLine("Color is Red, hashcode: " + COLOR.RED.GetHashCode());
break;
case COLOR.GREEN:
Console.WriteLine("Color is Green, hashcode: " + COLOR.GREEN.GetHashCode());
break;
case COLOR.YELLOW:
Console.WriteLine("Color is Yellow, hashcode: " + COLOR.YELLOW.GetHashCode());
break;
case COLOR.BLACK:
Console.WriteLine("Color is Black, hashcode: " + COLOR.BLACK.GetHashCode());
break;
case COLOR.WHITE:
Console.WriteLine("Color is White, hashcode: " + COLOR.WHITE.GetHashCode());
break;
case COLOR.BLUE:
Console.WriteLine("Color is Blue, hashcode: " + COLOR.BLUE.GetHashCode());
break;
}
}
static void Main(string[] args)
{
PrintHashCode(COLOR.RED);
PrintHashCode (COLOR.GREEN);
PrintHashCode (COLOR.BLUE);
}
}
Color is Red, hashcode: 0
Color is Green, hashcode: 1
Color is Blue, hashcode: 5
Press any key to continue . . .
Table of Contents