0
0
C Sharp (C#)programming~10 mins

Enum with switch pattern in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enum with switch pattern
Start
Define Enum
Assign Enum Variable
Switch on Enum
Case 1: Match Enum Value
Execute Case 1 Code
Case 2: Match Enum Value
Execute Case 2 Code
Default Case
Execute Default Code
End
The program defines an enum, assigns a value, then uses a switch to run code based on the enum value.
Execution Sample
C Sharp (C#)
enum Day { Monday, Tuesday, Wednesday }
Day today = Day.Tuesday;
switch (today) {
  case Day.Monday: Console.WriteLine("Start of week"); break;
  case Day.Tuesday: Console.WriteLine("Second day"); break;
  default: Console.WriteLine("Other day"); break;
}
This code prints a message depending on the day stored in the enum variable.
Execution Table
StepActionEnum ValueSwitch Case CheckedOutput
1Assign today = Day.TuesdayTuesday--
2Enter switch with today = TuesdayTuesday--
3Check case Day.MondayTuesdayMondayNo match, continue
4Check case Day.TuesdayTuesdayTuesdayMatch found
5Execute Console.WriteLine("Second day")TuesdayTuesdaySecond day
6Break from switchTuesday--
7End of switchTuesday--
💡 Switch ends after matching Day.Tuesday case and executing its code.
Variable Tracker
VariableStartAfter Step 1After Step 7
todayundefinedTuesdayTuesday
Key Moments - 3 Insights
Why does the switch skip the Day.Monday case?
Because the enum variable 'today' is set to Day.Tuesday, so the switch checks Day.Monday, finds no match (see step 3), and moves on.
What happens if no case matches the enum value?
The switch executes the default case code (not shown in this trace but would happen if no match), ensuring some code runs for unmatched values.
Why is there a break after each case?
The break stops the switch from checking further cases after a match, preventing multiple outputs (see step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'today' after step 1?
AMonday
BTuesday
CWednesday
Dundefined
💡 Hint
Check the 'Enum Value' column at step 1 in the execution_table.
At which step does the switch find a matching case?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for 'Match found' in the 'Output' column of the execution_table.
If 'today' was set to Day.Monday, which step would output 'Start of week'?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Consider when Console.WriteLine runs after matching the case in the execution_table.
Concept Snapshot
Enum with switch pattern in C#:
- Define enum with named values.
- Assign enum variable.
- Use switch(variable) to check enum cases.
- Each case matches an enum value.
- Use break to stop after match.
- Default case handles unmatched values.
Full Transcript
This example shows how to use an enum with a switch statement in C#. First, an enum named Day is defined with values Monday, Tuesday, and Wednesday. Then, a variable 'today' is assigned the value Day.Tuesday. The switch statement checks the value of 'today'. It first compares with Day.Monday and finds no match, then with Day.Tuesday and finds a match. It executes the code for Day.Tuesday, printing 'Second day'. The break statement stops further checking. If no cases matched, the default case would run. This pattern helps run different code based on enum values clearly and safely.