What if you could replace messy if-else chains with clean, natural-looking conditions?
Why Relational patterns in C Sharp (C#)? - Purpose & Use Cases
Imagine you have to check if a number falls within certain ranges using many separate if-else statements.
For example, checking if a score is less than 50, between 50 and 75, or above 75.
Writing many if-else conditions is slow and confusing.
It's easy to make mistakes like overlapping ranges or missing cases.
Also, the code becomes long and hard to read.
Relational patterns let you write clear, concise conditions that check if a value fits a range or relation.
This makes your code shorter, easier to understand, and less error-prone.
if (score < 50) { ... } else if (score >= 50 && score <= 75) { ... } else { ... }
switch (score) { case < 50: ...; break; case >= 50 and <= 75: ...; break; default: ...; break; }You can express complex range checks in a simple, readable way that looks like natural language.
Grading students by score ranges becomes easy and clear with relational patterns.
Manual range checks are long and error-prone.
Relational patterns simplify and clarify these checks.
They make your code easier to read and maintain.
Practice
is > 10 check in C#?Solution
Step 1: Understand the relational pattern syntax
The patternis > 10checks 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 AQuick Check:
Relational patternis > 10means greater than 10 [OK]
- Confusing > with >= or ==.
- Thinking it checks equality instead of comparison.
- Mixing up less than and greater than signs.
n is less than or equal to 5 using a relational pattern?Solution
Step 1: Identify correct relational pattern syntax
The relational pattern syntax usesisfollowed by the operator and value without spaces inside the operator, likeis <= 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 afteris, which is required.Final Answer:
if (n is <= 5) { } -> Option AQuick Check:
Relational pattern syntax:is <= value[OK]
- Using normal comparison without 'is'.
- Adding spaces inside the operator like '< ='.
- Omitting space after 'is'.
int x = 7;
if (x is >= 5 and < 10)
Console.WriteLine("In range");
else
Console.WriteLine("Out of range");Solution
Step 1: Evaluate the relational pattern condition
The conditionx is >= 5 and < 10checks 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, theConsole.WriteLine("In range")runs, printing "In range".Final Answer:
In range -> Option CQuick Check:
7 is between 5 and 10, so output is "In range" [OK]
- Thinking 'and' means 'or'.
- Ignoring one part of the condition.
- Assuming syntax error due to 'and'.
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;
}Solution
Step 1: Identify the syntax error in switch case patterns
Relational patterns in switch statements use syntax likecase > 90:, without theiskeyword. The code incorrectly usescase is > 90:andcase is >= 80 and < 90:.Step 2: Evaluate the options
The 'and' keyword is not allowed in switch case patterns is incorrect;andis allowed to combine patterns likecase >= 80 and < 90:. The pattern 'is > 90' should be 'case > 90:' without 'is' correctly identifies the misuse ofis. 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 DQuick Check:
Switch case relational pattern:case > value, nois[OK]
- Adding 'is' before relational operators in switch cases.
- Thinking 'and' cannot combine patterns in switch cases.
- Confusing switch statements with 'is' patterns.
string GetAgeGroup(int age) => age switch
{
< 13 => "Child",
>= 13 and < 20 => "Teen",
_ => "Adult"
};Solution
Step 1: Analyze the switch expression with relational patterns
The switch expression uses relational patterns correctly:< 13for "Child", combined pattern>= 13 and < 20for "Teen", and default for "Adult".Step 2: Confirm syntax correctness
The 'and' keyword is valid in switch expressions for combining patterns. No parentheses or '&&' are needed here.Final Answer:
Correct as is -> Option BQuick Check:
Switch expressions support 'and' for combined relational patterns [OK]
- Replacing 'and' with '&&' which is invalid in patterns.
- Adding unnecessary parentheses.
- Thinking switch expressions don't support relational patterns.
Ready to prove it?
Pass the test and this pattern moves unknown
