Skip to main content

15. Enums in C#

Level: Beginner

ℹ️ What You'll Learn
  • What an enum is
  • Why enums are better than random numbers or text
  • Create an enum
  • Store enum values in variables
  • Use enum with if and switch
  • 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

💻 Try It — Console App
💡 Paste into Program.cs and press F5⌥ GitHub
public enum FeeStatus
{
Paid,
Pending,
Partial
}

string studentName = "Sahasra Kumar";
FeeStatus feeStatus = FeeStatus.Partial;

Console.WriteLine($"Student: {studentName}");

switch (feeStatus)
{
case FeeStatus.Paid:
Console.WriteLine("Fees completed.");
break;

case FeeStatus.Pending:
Console.WriteLine("Fees not paid.");
break;

case FeeStatus.Partial:
Console.WriteLine("Some fees paid. Balance is pending.");
break;
}

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:

  1. Change FeeStatus.Partial to FeeStatus.Paid
  2. Add a new enum called ExamResult
  3. Add values: Pass, Fail, Absent
  4. Use switch to print a message for each result

ℹ️ Video Tutorial

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:

  • Present
  • Absent
  • Late

Create one variable and print a message using switch.

Quick Revision

QuestionAnswer
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

🎯 Q1: What is an enum in C#?

An enum is a named list of fixed choices.

🎯 Q2: Why use enums?

Enums make code readable and prevent invalid text values.

🎯 Q3: Give one real enum example.

FeeStatus with values Paid, Pending, and Partial.


🤖Use AI to Learn Faster
⚠️ Important for beginners: Do NOT use AI to write your code yet. Type every example yourself. Your brain learns by doing, not by reading AI output. Use AI only to explain and quiz you — not to code for you. Once you have strong fundamentals, AI becomes a powerful productivity tool for repetitive tasks.

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.

Next Article

Structs in C# ->

nexcoding.in