0
0
C Sharp (C#)programming

Enum with switch pattern in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
AA set of named constants
BA method to switch between classes
CA type of loop
DA variable that changes type
Which keyword is used to define an enum in C#?
Apattern
Bclass
Cenum
Dswitch
In switch pattern matching with enums, how do you match multiple cases together?
AUsing 'or' between cases
BUsing commas between cases
CUsing 'and' between cases
DUsing separate switch statements
What happens if you forget to handle an enum value in a switch statement?
AThe program crashes immediately
BThe compiler may warn you if you use pattern matching
CThe missing case is handled automatically
DThe switch statement ignores the enum
Which of these is a valid enum declaration?
Aenum Color: int { Red, Green, Blue }
Benum Color = { Red, Green, Blue }
Cenum Color() { Red, Green, Blue }
Denum Color { Red, Green, Blue }
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.