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

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

Choose your learning style9 modes available
Recall & Review
beginner
What is a relational pattern in C#?
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? <br>
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:
What will this code print if value = 75?<br>
switch(value) { case >= 90: Console.WriteLine("A"); break; case >= 70: Console.WriteLine("B"); break; default: Console.WriteLine("C"); }
AA
BB
CC
DNo output
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.
Which C# version introduced relational patterns?
AC# 7.0
BC# 8.0
CC# 10.0
DC# 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.
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.