0Pricing
C# Academy · Lesson

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

  1. Defining and Using Enums
  2. Flags Enums and Bitwise Combinations
  3. Enum Conversions and Parsing
  4. Iterating Enum Values
← Back to C# Academy