A relational pattern in C# is a way to compare a value against a condition using relational operators like <, <=, >, >= inside a pattern matching expression.
Click to reveal answer
beginner
Which relational operators can be used in C# relational patterns?
You can use <, <=, >, and >= operators in relational patterns to check if a value is less than, less than or equal to, greater than, or greater than or equal to another value.
Click to reveal answer
intermediate
How do relational patterns improve code readability?
Relational patterns let you write conditions directly inside switch or if statements in a clear and concise way, avoiding extra variables or complex if-else chains.
Click to reveal answer
beginner
Example: What does this C# code do?
case >= 90: Console.WriteLine("Excellent"); break;
This case matches when the value is greater than or equal to 90 and prints "Excellent". It uses a relational pattern to check the value directly.
Click to reveal answer
intermediate
Can relational patterns be combined with other patterns in C#?
Yes, relational patterns can be combined with logical patterns like 'and', 'or', and 'not' to create complex matching conditions.
Click to reveal answer
Which of these is a valid relational pattern in C#?
Acase > 10:
Bcase == 10:
Ccase != 10:
Dcase === 10:
✗ Incorrect
Relational patterns use operators like >, <, >=, <=. Equality (==) is a constant pattern, not a relational pattern. '===' is not valid in C#.
What will this code print if value = 75?
switch(value) { case >= 90: Console.WriteLine("A"); break; case >= 70: Console.WriteLine("B"); break; default: Console.WriteLine("C"); }
AA
BB
CC
DNo output
✗ Incorrect
75 is not >= 90, so first case fails. 75 is >= 70, so it prints "B".
Can you use relational patterns inside an if statement in C#?
AYes, but only with variables.
BNo, only in switch statements.
CYes, using the 'is' keyword.
DNo, relational patterns are not supported.
✗ Incorrect
Relational patterns can be used with 'is' in if statements, like 'if (x is > 10)'.
Which C# version introduced relational patterns?
AC# 7.0
BC# 8.0
CC# 10.0
DC# 9.0
✗ Incorrect
Relational patterns were introduced in C# 9.0.
What does this pattern mean? 'case < 0:'
AMatches values less than zero.
BMatches values equal to zero.
CMatches values greater than zero.
DMatches all values.
✗ Incorrect
The pattern matches any value less than zero.
Explain what relational patterns are and how they are used in C#.
Think about how you compare numbers in conditions.
You got /3 concepts.
Describe how relational patterns can be combined with logical patterns in C#.
Consider how you join multiple conditions in if statements.
You got /3 concepts.
Practice
(1/5)
1. What does the relational pattern is > 10 check in C#?
easy
A. If a value is greater than 10
B. If a value is equal to 10
C. If a value is less than 10
D. If a value is not equal to 10
Solution
Step 1: Understand the relational pattern syntax
The pattern is > 10 checks if a value is greater than 10.
Step 2: Compare with other options
Options A, B, and D check less than, equality, or inequality, which are different from > 10.
Final Answer:
If a value is greater than 10 -> Option A
Quick Check:
Relational pattern is > 10 means greater than 10 [OK]
Hint: Relational patterns use normal math signs like >, <, == [OK]
Common Mistakes:
Confusing > with >= or ==.
Thinking it checks equality instead of comparison.
Mixing up less than and greater than signs.
2. Which of the following is the correct syntax to check if an integer n is less than or equal to 5 using a relational pattern?
The relational pattern syntax uses is followed by the operator and value without spaces inside the operator, like is <= 5.
Step 2: Check each option
if (n is <= 5) { } uses correct syntax: if (n is <= 5). if (n <= 5) { } is normal comparison, not a relational pattern. if (n is < = 5) { } has a space between < and = which is invalid. if (n is <=5) { } misses a space after is, which is required.
Final Answer:
if (n is <= 5) { } -> Option A
Quick Check:
Relational pattern syntax: is <= value [OK]
Hint: Relational patterns need 'is' then operator with no spaces inside operator [OK]
Common Mistakes:
Using normal comparison without 'is'.
Adding spaces inside the operator like '< ='.
Omitting space after 'is'.
3. What will be the output of this C# code?
int x = 7;
if (x is >= 5 and < 10)
Console.WriteLine("In range");
else
Console.WriteLine("Out of range");
medium
A. No output
B. Out of range
C. In range
D. Compilation error
Solution
Step 1: Evaluate the relational pattern condition
The condition x is >= 5 and < 10 checks if x is between 5 and 9 inclusive. Since x = 7, it satisfies both parts.
Step 2: Determine which branch runs
Because the condition is true, the Console.WriteLine("In range") runs, printing "In range".
Final Answer:
In range -> Option C
Quick Check:
7 is between 5 and 10, so output is "In range" [OK]
Hint: Check both parts of 'and' relational pattern carefully [OK]
Common Mistakes:
Thinking 'and' means 'or'.
Ignoring one part of the condition.
Assuming syntax error due to 'and'.
4. Identify the error in this code snippet using relational patterns:
int score = 85;
switch (score)
{
case is > 90:
Console.WriteLine("Excellent");
break;
case is >= 80 and < 90:
Console.WriteLine("Good");
break;
default:
Console.WriteLine("Average");
break;
}
medium
A. The 'and' keyword is not allowed in switch case patterns
B. Missing braces around case blocks
C. Relational patterns cannot be used in switch cases
D. The pattern 'is > 90' should be 'case > 90:' without 'is'
Solution
Step 1: Identify the syntax error in switch case patterns
Relational patterns in switch statements use syntax like case > 90:, without the is keyword. The code incorrectly uses case is > 90: and case is >= 80 and < 90:.
Step 2: Evaluate the options
The 'and' keyword is not allowed in switch case patterns is incorrect; and is allowed to combine patterns like case >= 80 and < 90:. The pattern 'is > 90' should be 'case > 90:' without 'is' correctly identifies the misuse of is. Relational patterns cannot be used in switch cases is false; relational patterns are supported in switch cases. Missing braces around case blocks is wrong; braces are not required around case contents.
Final Answer:
The pattern 'is > 90' should be 'case > 90:' without 'is' -> Option D
Quick Check:
Switch case relational pattern: case > value, no is [OK]
Hint: No 'is' keyword needed for relational patterns in switch cases [OK]
Common Mistakes:
Adding 'is' before relational operators in switch cases.
Thinking 'and' cannot combine patterns in switch cases.
Confusing switch statements with 'is' patterns.
5. You want to write a method that returns "Child", "Teen", or "Adult" based on age using relational patterns. Which of these switch expressions correctly implements this?