Enum Conversions and Parsing
Convert between enums, integers, and strings.
Enum Conversions and Parsing is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);
}
}Validating with IsDefined
Enum.IsDefined confirms a number or name corresponds to a real member before you trust it.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
Console.WriteLine(Enum.IsDefined(typeof(Level), 1));
Console.WriteLine(Enum.IsDefined(typeof(Level), 9));
}
}Enum to String
ToString returns the member name, useful for display and logging.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
Console.WriteLine(Level.Medium.ToString());
}
}Parsing from a String
Enum.Parse turns a name back into an enum value, but it throws if the name is invalid.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
Level l = (Level)Enum.Parse(typeof(Level), "High");
Console.WriteLine(l);
}
}Generic Parse
The generic form Enum.Parse<T> avoids casting and reads cleanly.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
Level l = Enum.Parse<Level>("Medium");
Console.WriteLine(l);
}
}Safe Parsing with TryParse
Enum.TryParse returns a bool for success instead of throwing, which is safer for user input.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
if (Enum.TryParse<Level>("Low", out var result))
Console.WriteLine("Parsed: " + result);
}
}Handling Bad Input
When parsing fails, TryParse returns false so you can react gracefully.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
bool ok = Enum.TryParse<Level>("Unknown", out var result);
Console.WriteLine(ok ? result.ToString() : "Invalid input");
}
}Case-Insensitive Parsing
Pass true to ignore case when parsing user-provided text.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
Level l = Enum.Parse<Level>("high", true);
Console.WriteLine(l);
}
}TryParse Beats IsDefined for Names
For parsing names, TryParse handles both validity and conversion in one call.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
string input = "Medium";
if (Enum.TryParse<Level>(input, out var lvl) && Enum.IsDefined(typeof(Level), lvl))
Console.WriteLine("Valid: " + lvl);
}
}Round-Trip Conversions
You can convert enum to int and back, or to string and back, as long as the values are valid.
using System;
enum Level { Low, Medium, High }
class Program {
static void Main() {
Level start = Level.High;
int num = (int)start;
Level back = (Level)num;
Console.WriteLine(start == back);
}
}Quick Check
Test your knowledge of enum conversions and parsing.
Recap
Cast between an enum and its underlying integer with (int) and (EnumType), and use Enum.IsDefined to validate. ToString gives the member name.
Parse names back with Enum.Parse<T> (throws) or the safer Enum.TryParse<T> (returns a bool), optionally case-insensitively.
Frequently asked questions
Is the “Enum Conversions and Parsing” lesson free?
Yes — the full text of “Enum Conversions and Parsing” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Enum Conversions and Parsing”?
Convert between enums, integers, and strings. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enum Conversions and Parsing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining and Using Enums
- Flags Enums and Bitwise Combinations
- Enum Conversions and Parsing
- Iterating Enum Values