What if your code could catch missing cases automatically and never forget a status again?
Why Enum with switch pattern in C Sharp (C#)? - Purpose & Use Cases
Imagine you have a list of tasks, each with a status like Pending, InProgress, or Done. You want to perform different actions based on each status. Without enums and switch patterns, you might write many if-else statements checking strings or numbers manually.
This manual way is slow and confusing. It's easy to make mistakes like typos in status names or forget to handle a status. Adding new statuses means changing many places in your code, which can cause bugs and wastes time.
Using enums with switch patterns lets you clearly define all possible statuses in one place. The switch pattern lets you handle each status cleanly and safely. The compiler helps catch missing cases, so your code is easier to read, maintain, and extend.
if (status == "Pending") { /* do something */ } else if (status == "Done") { /* do something else */ } else { /* default */ }
switch (status) {
case Status.Pending: /* do something */ break;
case Status.Done: /* do something else */ break;
default: /* default */ break;
}This approach makes your code safer and clearer, so you can confidently add new statuses and handle them without fear of errors.
Think of a traffic light system where each light color is an enum. Using switch patterns, you can easily decide what action to take for Red, Yellow, or Green lights, making your program reliable and easy to update.
Manual checks with strings or numbers are error-prone and hard to maintain.
Enums group related values clearly, and switch patterns handle them safely.
This combination improves code clarity, safety, and ease of updates.
Practice
enum in C#?Solution
Step 1: Understand what enums represent
Enums are used to name a fixed set of related constants, like days of the week or colors.Step 2: Compare options with enum purpose
Only To give meaningful names to a set of related constant values correctly describes enums as naming related constant values.Final Answer:
To give meaningful names to a set of related constant values -> Option AQuick Check:
Enum = Named constants [OK]
- Thinking enums create classes
- Confusing enums with variables
- Assuming enums store unrelated data
Day with values Monday, Tuesday, and Wednesday?Solution
Step 1: Recall enum declaration syntax
In C#, enums are declared with the keywordenum, name, and curly braces listing values separated by commas.Step 2: Match syntax to options
enum Day { Monday, Tuesday, Wednesday } matches the correct syntax:enum Day { Monday, Tuesday, Wednesday }.Final Answer:
enum Day { Monday, Tuesday, Wednesday } -> Option BQuick Check:
Enum syntax uses braces and commas [OK]
- Using equals sign (=) after enum name
- Using colons or parentheses incorrectly
- Missing commas between values
enum Color { Red, Green, Blue }
Color c = Color.Green;
switch (c)
{
case Color.Red:
Console.WriteLine("Stop");
break;
case Color.Green:
Console.WriteLine("Go");
break;
case Color.Blue:
Console.WriteLine("Wait");
break;
}Solution
Step 1: Identify the enum value assigned
The variablecis assignedColor.Green.Step 2: Follow switch cases for
The switch matchesColor.GreenColor.Greenand prints "Go".Final Answer:
Go -> Option AQuick Check:
Switch on enum Green prints "Go" [OK]
- Confusing enum values with strings
- Missing break causing fall-through
- Assuming default case runs without match
enum Status { Pending, Approved, Rejected }
Status s = Status.Approved;
switch (s)
{
case Status.Pending:
Console.WriteLine("Waiting");
case Status.Approved:
Console.WriteLine("Accepted");
break;
case Status.Rejected:
Console.WriteLine("Denied");
break;
}Solution
Step 1: Check switch cases for breaks
The caseStatus.Pendinglacks abreak;, so execution falls through to the next case.Step 2: Understand fall-through effect
IfswasStatus.Pending, it would print both "Waiting" and "Accepted" unintentionally.Final Answer:
Missing break after case Status.Pending causes fall-through -> Option DQuick Check:
Each switch case needs a break [OK]
- Forgetting break causes multiple case runs
- Assuming enum declaration is wrong
- Thinking variable is uninitialized
Season with values Spring, Summer, Fall, Winter and returns a string describing the typical weather. Which switch expression correctly returns "Warm" for Summer and Spring, "Cool" for Fall, and "Cold" for Winter?Solution
Step 1: Check switch expression syntax
return season switch { Season.Spring => "Warm", Season.Summer => "Warm", Season.Fall => "Cool", Season.Winter => "Cold", _ => "Unknown" }; uses the modern C# switch expression with correct syntax and covers all enum values with correct returns.Step 2: Verify correct mapping of seasons to weather
Spring and Summer return "Warm", Fall returns "Cool", Winter returns "Cold", and a default case handles unknowns.Final Answer:
return season switch { Season.Spring => "Warm", Season.Summer => "Warm", Season.Fall => "Cool", Season.Winter => "Cold", _ => "Unknown" }; -> Option CQuick Check:
Switch expression with correct mappings [OK]
- Using incorrect syntax for multiple values in switch expression
- Mixing return values incorrectly
- Missing default case in switch expression
Ready to prove it?
Pass the test and this pattern moves unknown
