Constant patterns in C Sharp (C#) - Time & Space Complexity
Let's look at how some code runs in a way that doesn't change with input size.
We want to see what happens when the steps stay the same no matter how big the input is.
Analyze the time complexity of the following code snippet.
int MultiplyByTwo(int number)
{
int result = number * 2;
return result;
}
int value = 10;
int doubled = MultiplyByTwo(value);
Console.WriteLine(doubled);
This code multiplies a single number by two and prints the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One multiplication and one return.
- How many times: Exactly once, no loops or repeats.
Since the code does the same steps no matter the input size, the work stays steady.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (multiply, assign, return) |
| 100 | 3 (same steps) |
| 1000 | 3 (still the same steps) |
Pattern observation: The number of steps does not increase with input size; it stays constant.
Time Complexity: O(1)
This means the code takes the same amount of time no matter how big the input is.
[X] Wrong: "If the input number is bigger, the code will take longer to run."
[OK] Correct: The code does just one multiplication and return, so input size does not affect the steps.
Understanding constant time helps you recognize when code runs quickly and predictably, a useful skill in many coding tasks.
"What if we added a loop that runs 'number' times inside the function? How would the time complexity change?"
Practice
switch statement?Solution
Step 1: Understand constant pattern purpose
Constant patterns compare a value to a fixed constant to see if they are equal.Step 2: Apply to switch statement
In a switch, constant patterns match cases where the value equals the constant exactly.Final Answer:
It checks if the value exactly matches a fixed constant. -> Option BQuick Check:
Constant pattern = exact value check [OK]
- Thinking it checks ranges instead of exact values
- Confusing with type patterns
- Assuming it checks inequality
if statement in C#?Solution
Step 1: Recall constant pattern syntax
The syntax usesisfollowed by the constant value to check equality.Step 2: Check each option
if (x is 10) { } usesif (x is 10), which is correct. Others have syntax errors.Final Answer:
if (x is 10) { } -> Option CQuick Check:
Correct constant pattern syntax = if (x is 10) [OK]
- Using assignment '=' instead of 'is'
- Placing 'is' incorrectly
- Adding extra operators
int number = 5;
switch (number)
{
case 3:
Console.WriteLine("Three");
break;
case 5:
Console.WriteLine("Five");
break;
default:
Console.WriteLine("Other");
break;
}Solution
Step 1: Identify the value of 'number'
The variable 'number' is assigned the value 5.Step 2: Match the switch cases
The switch checks cases 3 and 5. Since number is 5, it matches case 5 and prints "Five".Final Answer:
Five -> Option AQuick Check:
number = 5 matches case 5 [OK]
- Choosing default instead of matching case
- Confusing case values
- Ignoring break statements
object obj = 10;
if (obj is 10)
{
Console.WriteLine("Ten");
}Solution
Step 1: Understand constant pattern with object
Constant patterns can be used with object type to check if it equals a constant value.Step 2: Check code correctness
The code usesobj is 10correctly to check if obj equals 10. This is valid syntax and logic.Final Answer:
No error, code is correct -> Option AQuick Check:
Constant pattern works with object type [OK]
- Assuming cast needed before pattern
- Thinking constant pattern needs variable
- Confusing with type patterns
var numbers = new List<int> {1, 7, 3, 7, 5};
int count = 0;
foreach (var n in numbers)
{
// Fill in the blank
}
Console.WriteLine(count);Solution
Step 1: Understand the goal
We want to count how many times 7 appears in the list.Step 2: Use constant pattern to check for 7
The correct constant pattern isif (n is 7)which checks if n equals 7, then increments count.Final Answer:
if (n is 7) count++; -> Option DQuick Check:
Constant pattern for 7 increments count [OK]
- Assigning count instead of incrementing
- Checking type instead of value
- Counting non-7 values
Ready to prove it?
Pass the test and this pattern moves unknown
