Enums & [Flags]
Define enums with explicit values and underlying types, convert and parse safely, and model combinable options with [Flags].
Enums & [Flags] is a free C# Academy lesson on CoddyKit — lesson 3 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Enums overview
Goal: Use enums for named constants and [Flags] for combinable options.
- Names map to numbers
- Underlying type (int by default)
- Cast, parse, and validate
- Bitwise combine with [Flags]
Basic enum
Enums are named constants. You can print the name or cast to the underlying number.
using System;
public enum OrderStatus
{
Pending = 0,
Processing = 1,
Shipped = 2,
Delivered = 3,
Cancelled = 4
}
public class Program
{
public static void Main(string[] args)
{
OrderStatus s = OrderStatus.Processing;
Console.WriteLine("Status = " + s);
Console.WriteLine("Numeric = " + (int)s);
}
}
Underlying type & cast
You may pick an underlying type (short, byte, etc.). Casting from numbers does not validate the value.
using System;
// Choose underlying type if needed (default is int)
public enum HttpCode : short
{
Ok = 200,
NotFound = 404
}
public class Program
{
public static void Main(string[] args)
{
short code = (short)HttpCode.Ok; // cast to number
HttpCode fromNum = (HttpCode)404; // cast from number (no validation)
Console.WriteLine("code=" + code);
Console.WriteLine("fromNum=" + fromNum);
}
}
Parsing & validation
Use Enum.TryParse to read from text and Enum.IsDefined to validate numeric values.
using System;
public enum Color
{
Red = 1, Green = 2, Blue = 3
}
public class Program
{
public static void Main(string[] args)
{
Color c;
bool ok = Enum.TryParse<Color>("green", true, out c); // ignore case
Console.WriteLine("Parsed? " + ok + " value=" + c);
// Validate a numeric value before casting
int raw = 4;
bool valid = Enum.IsDefined(typeof(Color), raw);
Console.WriteLine("Is 4 a defined Color? " + valid);
}
}
[Flags] in practice
Add [Flags] and assign power-of-two values. Combine with |, test with &, remove with ~ and &.
using System;
[Flags]
public enum FileAccessMode
{
None = 0,
Read = 1, // 0001
Write = 2, // 0010
Exec = 4 // 0100
}
public class Program
{
public static void Main(string[] args)
{
FileAccessMode m = FileAccessMode.Read | FileAccessMode.Write; // combine
Console.WriteLine("Mode = " + m); // prints "Read, Write"
// Test a flag (two common patterns)
bool canWrite = (m & FileAccessMode.Write) != 0;
Console.WriteLine("Can write? " + canWrite);
// Remove a flag
m = m & ~FileAccessMode.Write;
Console.WriteLine("After remove: " + m);
}
}
Enum tips
Tips:
- Give explicit values; keep them stable.
- For [Flags], use powers of two and include a None = 0.
- Validate inputs with Enum.TryParse/IsDefined.
- Avoid using raw integers in public APIs; prefer enums.
Flags attribute meaning
Recap
Recap: Use enums for clear, type-safe constants. Parse and validate safely. For combinable options, add [Flags] and use bitwise operators to set/test/remove flags.
Frequently asked questions
Is the “Enums & [Flags]” lesson free?
Yes — the full text of “Enums & [Flags]” is free to read here on the web, and the C# Academy course includes 3 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 “Enums & [Flags]”?
Define enums with explicit values and underlying types, convert and parse safely, and model combinable options with [Flags]. 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 3, so you can start here or from the beginning and move at your own pace.
How long does the “Enums & [Flags]” 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.