15. Enums in C#
Level: Beginner
- What an enum is
- Why enums are better than random numbers or text
- Create an enum
- Store enum values in variables
- Use enum with
ifandswitch - Use enums in the School Management System
An enum is a named list of fixed choices.
Use an enum when a value must come from a small known set.
Examples in a school system:
- Student status: Active, Inactive, Transferred
- Fee status: Paid, Pending, Partial
- User role: Admin, Teacher, Student
The Problem: Random Text Can Cause Mistakes
Without enum:
string status = "Actve";
Problem:
"Actve" is a spelling mistake.
C# will not catch it because it is just text.
Better:
StudentStatus status = StudentStatus.Active;
Now C# helps you choose from valid values.
Quick Definitions
- Enum - Named list of fixed values
- Enumeration - Full name of enum
- Enum value - One item inside the enum
- Magic string - Random text used in code, such as
"Active" - Magic number - Random number used in code, such as
1 - Switch - Good way to handle different enum values
Step 1: Create an Enum
public enum StudentStatus
{
Active,
Inactive,
Transferred,
Graduated
}
This creates four allowed values.
Step 2: Use an Enum Variable
StudentStatus status = StudentStatus.Active;
Console.WriteLine(status);
Output:
Active
Step 3: Use Enum in if/else
StudentStatus status = StudentStatus.Active;
if (status == StudentStatus.Active)
{
Console.WriteLine("Student can attend classes.");
}
else
{
Console.WriteLine("Student is not active.");
}
Step 4: Use Enum in switch
StudentStatus status = StudentStatus.Transferred;
switch (status)
{
case StudentStatus.Active:
Console.WriteLine("Student can attend classes.");
break;
case StudentStatus.Inactive:
Console.WriteLine("Student is inactive.");
break;
case StudentStatus.Transferred:
Console.WriteLine("Student moved to another school.");
break;
case StudentStatus.Graduated:
Console.WriteLine("Student completed studies.");
break;
}
Full Example: Fee Status
Expected output:
Student: Sahasra Kumar
Some fees paid. Balance is pending.
When You'll Use This in SMS
Enums make status fields clear.
Examples:
StudentStatus -> Active, Inactive, Transferred
FeeStatus -> Paid, Pending, Partial
UserRole -> Admin, Teacher, Student
ExamResult -> Pass, Fail, Absent
Without enums, spelling mistakes can enter the system.
With enums, C# limits the choices.
Try This Now
Run the fee status example. Then experiment:
- Change
FeeStatus.PartialtoFeeStatus.Paid - Add a new enum called
ExamResult - Add values:
Pass,Fail,Absent - Use
switchto print a message for each result
C# enums video coming soon on NexCoding YouTube. Subscribe for updates.
Common Mistakes
Mistake 1: Using text for fixed statuses
Use enum for fixed choices like status, role, or result.
Mistake 2: Using enum for unlimited values
Do not use enum for names, phone numbers, or addresses.
Mistake 3: Forgetting to handle all enum values in switch
Check every enum value when writing a switch.
Practice Task
Create an enum named AttendanceStatus with:
PresentAbsentLate
Create one variable and print a message using switch.
Quick Revision
| Question | Answer |
|---|---|
| What is an enum? | Named list of fixed values |
| Why use enum? | Avoid random text and spelling mistakes |
| Good enum example? | FeeStatus |
| Bad enum example? | StudentName |
| What works well with enum? | switch |
An enum is a named list of fixed choices.
Enums make code readable and prevent invalid text values.
FeeStatus with values Paid, Pending, and Partial.
Use ChatGPT, Claude, or Copilot to go deeper on C# enums. Try these prompts:
"Explain enums like I am a beginner""Give me 5 enum practice tasks""Explain enum with school fee status examples""Quiz me with 5 beginner questions about enums"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.