Expression-bodied methods in C Sharp (C#) - Time & Space Complexity
We want to understand how fast or slow expression-bodied methods run as the input size changes.
Specifically, we ask: how does the work grow when the method is called with bigger inputs?
Analyze the time complexity of the following code snippet.
public int Square(int x) => x * x;
public int SumArray(int[] numbers) => numbers.Sum();
public int MultiplyAll(int[] numbers) => numbers.Aggregate(1, (acc, val) => acc * val);
This code shows simple expression-bodied methods: one calculates a square, another sums an array, and the last multiplies all array elements.
Look for loops or repeated steps inside these methods.
- Primary operation: For
Square, just one multiplication. ForSumArrayandMultiplyAll, looping through the array. - How many times: The array methods repeat once for each element in the input array.
As the array gets bigger, the time to sum or multiply grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps for sum or multiply |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows directly with the number of items in the array.
Time Complexity: O(n)
This means the time to run grows in a straight line with the input size.
[X] Wrong: "Expression-bodied methods always run faster because they use less code."
[OK] Correct: The syntax is shorter but the work done depends on what the method does, not how it is written.
Understanding how expression-bodied methods perform helps you explain your code clearly and shows you know how to think about efficiency.
"What if we changed the array methods to use recursion instead of built-in functions? How would the time complexity change?"
Practice
Solution
Step 1: Understand expression-bodied methods
Expression-bodied methods use the => symbol to return a single expression directly, making code shorter.Step 2: Identify the purpose
The main purpose is to simplify simple methods by writing them in one line with clear syntax.Final Answer:
To write simple methods in a shorter and clearer way using the => syntax -> Option DQuick Check:
Expression-bodied methods = shorter, clearer methods [OK]
- Confusing expression-bodied methods with variable declarations
- Thinking they handle exceptions
- Believing they create classes
n?Solution
Step 1: Recall expression-bodied method syntax
Expression-bodied methods use => followed directly by the expression without braces or return keyword.Step 2: Check each option
public int Square(int n) => n * n; correctly uses => n * n; without braces or return. public int Square(int n) { return n * n; } uses normal method syntax. Options C and D have syntax errors.Final Answer:
public int Square(int n) => n * n; -> Option AQuick Check:
Correct syntax = public int Square(int n) => n * n; [OK]
- Adding braces {} around the expression
- Using return keyword with =>
- Missing semicolon at the end
class Program {
static int Double(int x) => x * 2;
static void Main() {
Console.WriteLine(Double(5));
}
}Solution
Step 1: Understand the method Double
The method Double takes an integer x and returns x multiplied by 2 using expression-bodied syntax.Step 2: Calculate the output for Double(5)
Double(5) returns 5 * 2 = 10, which is printed by Console.WriteLine.Final Answer:
10 -> Option BQuick Check:
Double(5) = 10 [OK]
- Confusing output with input value
- Expecting syntax error due to unfamiliar syntax
- Ignoring return value
public int Add(int a, int b) => { return a + b; };Solution
Step 1: Analyze expression-bodied method syntax
Expression-bodied methods must have a single expression after => without braces or return keyword.Step 2: Identify the fix
Remove braces and return keyword to fix syntax: public int Add(int a, int b) => a + b;Final Answer:
Remove braces and return keyword: public int Add(int a, int b) => a + b; -> Option CQuick Check:
Expression-bodied method = single expression after => [OK]
- Using braces {} with expression-bodied methods
- Including return keyword after =>
- Thinking =>> is valid syntax
IsEven that returns true if an integer is even, and false otherwise. Which of the following implementations is correct?Solution
Step 1: Understand the method goal
The method should return true if n is even (n % 2 == 0), false otherwise.Step 2: Check each option
public bool IsEven(int n) => n % 2 == 0; correctly uses expression-bodied syntax with the expression n % 2 == 0. public bool IsEven(int n) => { if (n % 2 == 0) return true; else return false; }; uses braces and return, which is invalid for expression-bodied methods. public bool IsEven(int n) => return n % 2 == 0; incorrectly uses return keyword after =>. public bool IsEven(int n) => n / 2 == 0; incorrectly checks n / 2 == 0, which is wrong logic.Final Answer:
public bool IsEven(int n) => n % 2 == 0; -> Option AQuick Check:
IsEven uses n % 2 == 0 with => [OK]
- Using braces and return in expression-bodied methods
- Adding return keyword after =>
- Incorrect logic like n / 2 == 0
Ready to prove it?
Pass the test and this pattern moves unknown
