Defining and Using Enums
Create enums with explicit and implicit values.
Defining and Using Enums is a free C# Academy lesson on CoddyKit — lesson 1 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.
What is an Enum?
An enum is a named set of related constants. It makes code readable by replacing magic numbers with meaningful names.
using System;
enum Direction { North, East, South, West }
class Program {
static void Main() {
Direction d = Direction.East;
Console.WriteLine(d);
}
}Declaring an Enum
You declare an enum with the enum keyword and list its members. By default the first is 0, the next 1, and so on.
using System;
enum Day { Mon, Tue, Wed }
class Program {
static void Main() {
Console.WriteLine((int)Day.Mon);
Console.WriteLine((int)Day.Wed);
}
}Custom Underlying Values
You can assign explicit numbers. Unassigned members continue from the previous value.
using System;
enum Status { Active = 1, Paused = 5, Stopped }
class Program {
static void Main() {
Console.WriteLine((int)Status.Stopped);
}
}Choosing an Underlying Type
Enums use int by default, but you can pick another integral type such as byte to save space.
using System;
enum Level : byte { Low = 1, High = 200 }
class Program {
static void Main() {
Console.WriteLine((byte)Level.High);
}
}Enums in Variables
Use an enum type like any other type for variables, parameters, and fields.
using System;
enum Color { Red, Green, Blue }
class Program {
static void Describe(Color c) {
Console.WriteLine("Color is " + c);
}
static void Main() {
Describe(Color.Green);
}
}Enums in switch
Enums pair beautifully with switch for clear, exhaustive branching.
using System;
enum Signal { Stop, Go, Caution }
class Program {
static void Main() {
Signal s = Signal.Go;
switch (s) {
case Signal.Stop: Console.WriteLine("Halt"); break;
case Signal.Go: Console.WriteLine("Move"); break;
default: Console.WriteLine("Slow"); break;
}
}
}Comparing Enum Values
Enum values compare with == and ordering operators based on their underlying numbers.
using System;
enum Priority { Low, Medium, High }
class Program {
static void Main() {
Console.WriteLine(Priority.High > Priority.Low);
}
}Default Enum Value
An uninitialized enum equals the member with value 0. It is good practice to make 0 a sensible default like None.
using System;
enum Mode { None, Fast, Slow }
class Program {
static void Main() {
Mode m = default;
Console.WriteLine(m);
}
}Printing Enum Names
Calling ToString or printing an enum shows the member name, which is far clearer than a raw number.
using System;
enum Fruit { Apple, Banana, Cherry }
class Program {
static void Main() {
Console.WriteLine(Fruit.Banana.ToString());
}
}Enums Improve Readability
Compared with passing bare integers, enums document intent and prevent invalid values.
using System;
enum Size { Small, Medium, Large }
class Program {
static int PriceFor(Size s) {
return s == Size.Large ? 12 : 8;
}
static void Main() {
Console.WriteLine(PriceFor(Size.Large));
}
}Where to Declare Enums
Enums can sit at namespace level or inside a class. Keep them near the code that uses them.
using System;
class Program {
enum Phase { Start, Run, End }
static void Main() {
Console.WriteLine(Phase.Run);
}
}Quick Check
Test your understanding of enums.
Recap
An enum defines a set of named constants, improving readability over magic numbers. Members default to 0, 1, 2..., but you can assign explicit values and even change the underlying integral type.
Enums work great in variables, switches, and comparisons, print their member names, and default to the 0-valued member.
Frequently asked questions
Is the “Defining and Using Enums” lesson free?
Yes — the full text of “Defining and Using Enums” 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 “Defining and Using Enums”?
Create enums with explicit and implicit values. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining and Using Enums” 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