Relational patterns in C Sharp (C#) - Time & Space Complexity
We want to understand how fast a program runs when it uses relational patterns in C#.
Specifically, we ask: how does the program's work grow when the input changes size?
Analyze the time complexity of the following code snippet.
int[] numbers = {1, 5, 10, 15, 20};
int threshold = 10;
foreach (int num in numbers)
{
if (num is > threshold)
{
Console.WriteLine($"{num} is greater than {threshold}");
}
}
This code checks each number to see if it is greater than a threshold and prints it if true.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the array.
- How many times: Once for every number in the array.
As the list of numbers gets bigger, the program checks more numbers one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the program takes longer in a straight line as the input list grows.
[X] Wrong: "Relational patterns make the program faster because they are special checks."
[OK] Correct: Relational patterns are just a way to write conditions clearly; the program still checks each item one by one.
Understanding how loops and conditions affect speed helps you explain your code clearly and shows you know how programs grow with input.
"What if we replaced the array with a sorted list and stopped checking after the first number greater than the threshold? How would the time complexity change?"
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
