Enum with switch pattern in C Sharp (C#) - Time & Space Complexity
We want to understand how the time it takes to run code with an enum and switch pattern changes as the number of enum cases grows.
Specifically, how does checking each enum value with a switch affect performance?
Analyze the time complexity of the following code snippet.
enum Color { Red, Green, Blue, Yellow }
string GetColorMessage(Color color)
{
return color switch
{
Color.Red => "Stop",
Color.Green => "Go",
Color.Blue => "Calm",
Color.Yellow => "Caution",
_ => "Unknown"
};
}
This code returns a message based on the color enum using a switch expression.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The switch checks the enum value once to find the matching case.
- How many times: Exactly one check per call; no loops or repeated traversals.
Since the switch checks only once per call, the time does not grow with the number of enum values in a simple way.
| Input Size (number of enum cases) | Approx. Operations per call |
|---|---|
| 4 | 1 check |
| 10 | 1 check |
| 100 | 1 check |
Pattern observation: The operation count stays about the same for each call, regardless of enum size.
Time Complexity: O(1)
This means the time to get the message stays constant no matter how many enum values exist.
[X] Wrong: "More enum cases always make the switch slower because it checks each case one by one."
[OK] Correct: The compiler often optimizes switch statements on enums to jump directly to the matching case, so it does not check all cases one by one.
Understanding how switch statements work with enums helps you explain code efficiency clearly and shows you know how language features affect performance.
"What if we replaced the switch with a loop that checks each enum case one by one? How would the time complexity change?"
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
