Enum Conversions and Parsing
Convert between enums, integers, and strings.
Enum to Integer
Cast an enum to its underlying type with (int) to get the numeric value.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
int n = (int)Level.High;
Console.WriteLine(n);
}
}Integer to Enum
Cast a number back to the enum type. The value need not be a defined member, so check when needed.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
Level l = (Level)2;
Console.WriteLine(l);
}
}All lessons in this course
- Defining and Using Enums
- Flags Enums and Bitwise Combinations
- Enum Conversions and Parsing
- Iterating Enum Values