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

Constant patterns in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
AChecks if a value equals a specific constant
BDeclares a constant variable
CCreates a new object
DLoops through a collection
Which keyword is used with constant patterns in C# to test a value?
Anew
Bis
Cvar
Dfor
In a switch statement, how do you write a constant pattern for the number 10?
Acase x == 10:
Bcase const 10:
Ccase 10:
Dcase (10):
What will happen if no constant pattern matches in a switch expression?
AThe default case runs if provided
BThe program crashes
CThe first case runs anyway
DThe switch is ignored
Can constant patterns be used with strings in C#?
AOnly in loops
BNo, only numbers
COnly with variables
DYes, strings can be constant patterns
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.