Recall & Review
beginner
What is an enum in C#?
An enum (short for enumeration) is a special data type that lets you define a set of named constants. It helps make code more readable by using names instead of numbers.
Click to reveal answer
beginner
How does the switch pattern matching work with enums in C#?
Switch pattern matching lets you check an enum value and run code based on which named constant it matches, making the code clear and easy to follow.
Click to reveal answer
intermediate
Why use switch pattern matching instead of if-else with enums?
Switch pattern matching is cleaner and easier to read when checking multiple enum values. It also helps the compiler warn you if you forget to handle some enum cases.
Click to reveal answer
beginner
Write a simple enum declaration for days of the week in C#.
enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
Click to reveal answer
beginner
Show a switch pattern matching example using the DayOfWeek enum to print if it's a weekend or weekday.
DayOfWeek day = DayOfWeek.Saturday;
switch (day)
{
case DayOfWeek.Saturday or DayOfWeek.Sunday:
Console.WriteLine("It's the weekend!");
break;
default:
Console.WriteLine("It's a weekday.");
break;
}
Click to reveal answer
What does an enum represent in C#?
✗ Incorrect
An enum defines a set of named constants to represent related values clearly.
Which keyword is used to define an enum in C#?
✗ Incorrect
The 'enum' keyword declares an enumeration type.
In switch pattern matching with enums, how do you match multiple cases together?
✗ Incorrect
You can use 'or' to combine multiple enum cases in one case block.
What happens if you forget to handle an enum value in a switch statement?
✗ Incorrect
With pattern matching, the compiler can warn about unhandled enum cases.
Which of these is a valid enum declaration?
✗ Incorrect
The correct syntax uses 'enum Name { values }' without '=' or parentheses.
Explain how to use an enum with a switch pattern in C# to handle different cases.
Think about how you check the enum value and run code for each named constant.
You got /4 concepts.
Describe the benefits of using switch pattern matching with enums compared to if-else statements.
Consider how the code looks and how the compiler helps you.
You got /4 concepts.