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#.

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

No comments:

Post a Comment