Recall & Review
beginner
What is a constant pattern in C#?
A constant pattern checks if a value matches a specific constant value. It is used in pattern matching to compare values directly.
Click to reveal answer
beginner
How do you write a constant pattern in a C#
switch expression?You write the constant value directly before the =>, like
5 => "five", or "hello" => "hello",.Click to reveal answer
beginner
What happens if a constant pattern does not match the value?
The pattern matching moves on to the next pattern or the
default case if no match is found.Click to reveal answer
beginner
Can constant patterns be used with
is expressions in C#?Yes, you can use constant patterns with
is to check if a variable equals a constant, like if (x is 10).Click to reveal answer
beginner
Example: What will this code print?<br>
int x = 3;<br>string result = x switch {<br> 1 => "One",<br> 2 => "Two",<br> 3 => "Three",<br> _ => "Other"<br>};<br>Console.WriteLine(result);It will print Three because the value 3 matches the constant pattern
3 in the switch expression.Click to reveal answer
What does a constant pattern do in C#?
✗ Incorrect
A constant pattern compares a value to a fixed constant to check for equality.
Which keyword is used with constant patterns in C# to test a value?
✗ Incorrect
The 'is' keyword is used with constant patterns to check if a value matches a constant.
In a switch statement, how do you write a constant pattern for the number 10?
✗ Incorrect
You write 'case 10:' to match the constant value 10.
What will happen if no constant pattern matches in a switch expression?
✗ Incorrect
If no constant pattern matches, the default case runs if it exists.
Can constant patterns be used with strings in C#?
✗ Incorrect
Constant patterns can match string values directly.
Explain what a constant pattern is and how it is used in C# pattern matching.
Think about how you check if a value equals a fixed number or text.
You got /3 concepts.
Describe how a switch expression uses constant patterns to decide which code to run.
Imagine choosing an option based on exact matches.
You got /3 concepts.