Tuesday, August 11, 2020

How to get Int value from enum in C#

Parsing the enumerator value to integer will give us the desired result. See below the sample code to get integer value from enumerator in C#.

class Program
{
    static void Main(string[] args)
    {
        int intVal = (int)LevelEnum.Three;
        Console.WriteLine(intVal);
    }
}
public enum LevelEnum
{
    One = 1,
    Two = 2,
    Three = 3,
    Four = 4
}
The result will be 3.

No comments:

Post a Comment