Expression-bodied lambdas in C Sharp (C#) - Time & Space Complexity
We want to understand how the time it takes to run expression-bodied lambdas changes as the input grows.
Specifically, how does the number of operations scale when using these lambdas in code?
Analyze the time complexity of the following code snippet.
Func square = x => x * x;
int[] numbers = {1, 2, 3, 4, 5};
foreach (var num in numbers)
{
Console.WriteLine(square(num));
}
This code defines a simple expression-bodied lambda to square a number and applies it to each item in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The foreach loop that calls the lambda for each number.
- How many times: Once for each element in the array.
As the number of elements increases, the lambda runs once per element.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to the lambda |
| 100 | 100 calls to the lambda |
| 1000 | 1000 calls to the lambda |
Pattern observation: The total work grows directly with the number of items.
Time Complexity: O(n)
This means the time to complete grows in a straight line as the input size grows.
[X] Wrong: "Expression-bodied lambdas run faster and reduce time complexity."
[OK] Correct: The lambda syntax is just a shorter way to write code; it does not change how many times the operation runs.
Understanding how lambdas affect time helps you explain your code clearly and shows you know how code scales with input size.
"What if the lambda called another function inside it? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand expression-bodied lambda syntax
Expression-bodied lambdas use the => operator followed by a single expression, simplifying function syntax.Step 2: Identify the purpose
This syntax is used to write short functions without braces or explicit return statements.Final Answer:
To write a short function with a single expression without braces or return keyword -> Option AQuick Check:
Expression-bodied lambda = short single-expression function [OK]
- Confusing with variable declarations
- Thinking it creates classes or handles exceptions
- Assuming it requires braces and return keyword
a and b?Solution
Step 1: Identify expression-bodied lambda syntax
Expression-bodied lambdas use => followed by a single expression without braces or return keyword.Step 2: Check each option
(a, b) => a + b correctly uses (a, b) => a + b without braces or return. (a, b) => { return a + b; } uses braces and return (not expression-bodied). (a, b) => a + b; has a semicolon after expression (not needed). (a, b) => { a + b } uses braces without return (invalid).Final Answer:
(a, b) => a + b -> Option AQuick Check:
Expression-bodied lambda syntax = (params) => expression [OK]
- Adding braces and return keyword
- Including semicolon after expression
- Using braces without return keyword
Func<int, int> square = x => x * x; Console.WriteLine(square(4));
Solution
Step 1: Understand the lambda function
The lambda expression x => x * x defines a function that returns the square of x.Step 2: Evaluate the function call
Calling square(4) computes 4 * 4 = 16.Final Answer:
16 -> Option CQuick Check:
4 squared = 16 [OK]
- Confusing output with input
- Expecting compilation error
- Misreading the lambda expression
Func<int, int> increment = x => { x + 1; };Solution
Step 1: Analyze the lambda syntax
The lambda uses braces, so it must have an explicit return statement to return a value.Step 2: Identify the missing return
Inside braces, the expression x + 1 is evaluated but not returned, causing a compile error.Final Answer:
Missing return keyword inside braces -> Option DQuick Check:
Braces require return keyword [OK]
- Assuming braces are disallowed
- Thinking semicolon after expression is invalid
- Expecting multiple parameters
var numbers = new List<int> {1, 2, 3, 4, 5};
var result = numbers.Where( /* lambda here */ ).Select(x => x * x).ToList();Solution
Step 1: Understand filtering condition
We want to keep odd numbers, so the condition is x % 2 != 0.Step 2: Choose correct lambda syntax
x => x % 2 != 0 uses expression-bodied lambda without braces, correctly returning the condition. x => x % 2 == 0 keeps even numbers (wrong). x => { return x % 2 != 0; } uses braces with return (valid but not expression-bodied). x => { x % 2 != 0; } uses braces without return (invalid).Final Answer:
x => x % 2 != 0 -> Option BQuick Check:
Filter odds with expression-bodied lambda [OK]
- Using braces without return
- Choosing wrong condition for filtering
- Mixing expression-bodied with block-bodied syntax
Ready to prove it?
Pass the test and this pattern moves unknown
