C# Enum Example: The Best Way to Use Named Constants in Your Code
An Enum (short for Enumeration) in C# is a value type that lets you define a set of named constants. It is used when you have a fixed number of possible values and want to make your code more readable and manageable.
Instead of using magic numbers or strings, Enums provide meaningful names to values. This makes your code easier to understand and less error-prone.
What You Are Going to Learn in This Lesson?
✅ What an Enum is in C#
✅ Why using Enums makes your code cleaner
✅ A real-world example to make it easy
✅ A C# Enum Example with complete code and explanation
Key Points About Enums:
✅ Enums store named constants (like Small
, Medium
, Large
for sizes).
✅ Internally, each constant has an integer value (by default, starting from 0
).
✅ You can assign custom values to Enum members if needed.
✅ Enums make code cleaner and prevent errors from using random numbers or strings.
Example
Imagine you’re ordering a coffee ☕. The waiter asks, “What size?” You can say:
- Small
- Medium
- Large
But what if the system stored sizes using random numbers? 🤯 It might be:
- 1 for Small
- 2 for Medium
- 3 for Large
You’d have to remember what 1, 2, and 3 mean—which is not fun. That’s exactly why Enums exist in C#! Instead of using random numbers or strings, Enums let you use meaningful names.
Why Should You Use Enums?
Here’s why Enums are super useful:
✅ Makes code readable – Instead of using 1
, 2
, or 3
, you use Small
, Medium
, or Large
.
✅ Prevents mistakes – No more typos in string values.
✅ Easy to update – If you add a new option, it’s simple to manage.
✅ Better than magic numbers – No more weird numbers floating in your code.
Real-World Scenario: Coffee Ordering System
Let’s say you’re building a coffee shop ordering system. You need to handle different coffee sizes without using random numbers. Enums to the rescue! 🚀
C# Enum Example: Coffee Size Selection
Step 1: Define an Enum
First, create an Enum to represent coffee sizes.
using System;
enum CoffeeSize
{
Small, // 0
Medium, // 1
Large // 2
}
class Program
{
static void Main()
{
CoffeeSize myCoffee = CoffeeSize.Medium;
Console.WriteLine("You ordered a " + myCoffee + " coffee.");
}
}
Output:
You ordered a Medium coffee.
What’s Happening Here?
1️⃣ We defined an Enum named CoffeeSize
.
2️⃣ It has three values: Small
, Medium
, and Large
.
3️⃣ We assigned Medium
to myCoffee
.
4️⃣ When we print it, we see “Medium” instead of a number.
Using Enums with User Input
Now, let’s take input from the user and convert it into an Enum.
using System;
enum CoffeeSize
{
Small = 1,
Medium = 2,
Large = 3
}
class Program
{
static void Main()
{
Console.WriteLine("Choose a coffee size: 1 for Small, 2 for Medium, 3 for Large");
int choice = Convert.ToInt32(Console.ReadLine());
CoffeeSize selectedSize = (CoffeeSize)choice;
Console.WriteLine("You ordered a " + selectedSize + " coffee.");
}
}
Possible Outputs:
User enters 1:
You ordered a Small coffee.
User enters 3:
You ordered a Large coffee.
What Changed Here?
✅ We assigned custom values (1, 2, 3
) to the Enum instead of starting from 0
.
✅ The user inputs a number, and we convert it to an Enum.
✅ The system prints a human-friendly name instead of a number.
Bonus: Using Enums in a Switch Statement
We can use Enums in a switch-case to handle different scenarios easily!
using System;
enum CoffeeSize
{
Small,
Medium,
Large
}
class Program
{
static void Main()
{
CoffeeSize myCoffee = CoffeeSize.Large;
switch (myCoffee)
{
case CoffeeSize.Small:
Console.WriteLine("A small coffee? Great choice for a quick sip!");
break;
case CoffeeSize.Medium:
Console.WriteLine("Medium coffee? Perfect balance!");
break;
case CoffeeSize.Large:
Console.WriteLine("Large coffee? You must need extra energy!");
break;
}
}
}
Output:
Large coffee? You must need extra energy!
Why Use Switch with Enums?
✅ Super easy to read – No need to compare numbers manually.
✅ More flexible – You can handle each case separately.
✅ Fewer bugs – No risk of using wrong values.
Conclusion
Enums make your life easier! Instead of using random numbers or strings, you can assign meaningful names to values. This makes your code clean, readable, and error-free.
Now, go ahead and use Enums in your own projects! And hey, if you have difficulty or a question, drop a comment. We will be happy to help you. 😊
Next What?
In the next lesson, you’ll learn about Structures (struct) in C#—a cool way to create lightweight data types! Stay tuned! 🚀