Func delegate type in C Sharp (C#) - Time & Space Complexity
We want to understand how the time it takes to run code using the Func delegate changes as the input size grows.
Specifically, how does calling a Func repeatedly affect performance?
Analyze the time complexity of the following code snippet.
Func<int, int> square = x => x * x;
int n = 10; // example size
int[] numbers = new int[n];
int[] results = new int[n];
for (int i = 0; i < n; i++)
{
results[i] = square(numbers[i]);
}
This code uses a Func delegate to square each number in an array and stores the results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the Func delegate inside a for loop.
- How many times: Exactly once for each element in the input array, so n times.
Each time we add one more number, we call the Func one more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to Func |
| 100 | 100 calls to Func |
| 1000 | 1000 calls to Func |
Pattern observation: The number of operations grows directly with the input size.
Time Complexity: O(n)
This means the time to complete the task grows in a straight line as the input size increases.
[X] Wrong: "Using a Func delegate makes the code slower in a way that changes the time complexity."
[OK] Correct: Calling a Func delegate is just like calling a normal method once per item, so it does not change how the total time grows with input size.
Understanding how delegates like Func affect performance helps you explain your code choices clearly and shows you know how code runs as data grows.
"What if the Func delegate called another function inside a loop? How would the time complexity change?"
Practice
Func delegate type in C#?Solution
Step 1: Understand what
FuncrepresentsFuncis a delegate type designed to hold references to methods that return a value.Step 2: Differentiate from other delegates
UnlikeAction, which stores methods without return values,Funcalways returns a value.Final Answer:
To store methods that return a value -> Option AQuick Check:
Funcstores methods with return values = C [OK]
- Confusing Func with Action delegate
- Thinking Func can store void methods
- Assuming Func defines classes
Func delegate that takes two integers and returns an integer?Solution
Step 1: Understand Func generic parameters
The last type parameter is the return type; previous ones are input types. For two integers input and one integer output, useFunc.Step 2: Check lambda expression matches parameters
The lambda(x, y) => x + ytakes two inputs and returns their sum, matching the delegate.Final Answer:
Func<int, int, int> add = (x, y) => x + y; -> Option BQuick Check:
Two inputs + one output = Func<int, int, int> [OK]
- Using wrong number of generic parameters
- Mixing input and output types order
- Incorrect lambda parameter count
Func<int, int, int> multiply = (a, b) => a * b; int result = multiply(3, 4); Console.WriteLine(result);
Solution
Step 1: Understand the Func delegate and lambda
The delegatemultiplytakes two integers and returns their product.Step 2: Calculate the result of multiply(3, 4)
3 multiplied by 4 equals 12.Final Answer:
12 -> Option AQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Expecting syntax error
Func<int, int> square = x => x * x; int result = square(5, 2); Console.WriteLine(result);
Solution
Step 1: Check Func delegate signature
Func<int, int>expects one integer input and returns an integer.Step 2: Check method call arguments
The callsquare(5, 2)passes two arguments, but the delegate expects only one.Final Answer:
Too many arguments passed to the Func delegate -> Option DQuick Check:
One input expected, two given = error [OK]
- Passing wrong number of arguments
- Confusing input and output types
- Assuming lambda needs explicit return
Func delegate that takes a string and returns its length only if the string is not null or empty; otherwise, it returns zero. Which of the following implementations correctly achieves this?Solution
Step 1: Understand the requirement
The function must return 0 if the string is null or empty, else return its length.Step 2: Analyze each option
Func lengthFunc = s => string.IsNullOrEmpty(s) ? 0 : s.Length; usesstring.IsNullOrEmptyto check both conditions and returns 0 or length accordingly, which is correct. Func lengthFunc = s => s.Length; does not check for null or empty and may throw an error. Func lengthFunc = s => { if (s == null) return 0; else return s.Length; }; checks only for null, not empty. Func lengthFunc = s => s == "" ? s.Length : 0; returns length only if string is empty, which is opposite.Final Answer:
Func<string, int> lengthFunc = s => string.IsNullOrEmpty(s) ? 0 : s.Length; -> Option CQuick Check:
Check null or empty with IsNullOrEmpty = Func lengthFunc = s => string.IsNullOrEmpty(s) ? 0 : s.Length; [OK]
- Not checking empty string
- Only checking null but not empty
- Returning wrong value for empty string
Ready to prove it?
Pass the test and this pattern moves unknown
