Action delegate type in C Sharp (C#) - Time & Space Complexity
We want to understand how the time it takes to run code using the Action delegate changes as the input grows.
How does the number of times the delegate runs affect the total work done?
Analyze the time complexity of the following code snippet.
Action printNumber = n => Console.WriteLine(n);
void RunActionMultipleTimes(int count, Action action)
{
for (int i = 0; i < count; i++)
{
action(i);
}
}
RunActionMultipleTimes(5, printNumber);
This code runs an Action delegate multiple times, printing numbers from 0 up to count - 1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the Action delegate inside a for loop.
- How many times: The loop runs exactly
counttimes, so the delegate is calledcounttimes.
As the input count grows, the number of times the delegate runs grows the same way.
| Input Size (count) | Approx. Operations |
|---|---|
| 10 | 10 calls to the Action delegate |
| 100 | 100 calls to the Action delegate |
| 1000 | 1000 calls to the Action delegate |
Pattern observation: The total work grows directly with the input size. Double the input, double the calls.
Time Complexity: O(n)
This means the time to run the code grows in a straight line with the number of times the Action delegate is called.
[X] Wrong: "Calling an Action delegate inside a loop is instant and does not add to time."
[OK] Correct: Each call to the delegate runs code and takes time, so more calls mean more total time.
Understanding how delegates affect time helps you explain how your code scales when using callbacks or events, a common real-world skill.
"What if the Action delegate itself contains a loop that runs n times? How would the time complexity change?"
Practice
Action delegate type in C#?Solution
Step 1: Understand what Action represents
Actionis a delegate type used to represent methods that do not return a value (void) but can take parameters.Step 2: Compare options with the definition
To represent methods that return void and can take parameters correctly states thatActionrepresents methods returning void and can take parameters. Other options mention return values or asynchronous behavior, which are incorrect.Final Answer:
To represent methods that return void and can take parameters -> Option AQuick Check:
Action = void methods with parameters [OK]
- Confusing Action with Func which returns a value
- Thinking Action is only for methods without parameters
- Assuming Action is for async methods only
Action delegate that takes two integer parameters?Solution
Step 1: Recall Action delegate syntax
TheActiondelegate syntax for parameters isAction<T1, T2>for two parameters.Step 2: Analyze each option
Action myAction; correctly declares anActionwith two int parameters. Action<int> myAction; has only one parameter. Action myAction(int, int); is invalid syntax. Action myAction() => void; is incorrect syntax and return type usage.Final Answer:
Action myAction; -> Option CQuick Check:
Two parameters use Action<int, int> [OK]
- Using parentheses like a method instead of generic syntax
- Declaring return type with Action (it always returns void)
- Confusing Action with Func syntax
Action<string> greet = name => Console.WriteLine($"Hello, {name}!");
greet("Alice");Solution
Step 1: Understand the Action delegate usage
Thegreetvariable is anAction<string>delegate that takes a string and prints a greeting with that string.Step 2: Trace the method call
Callinggreet("Alice")runs the lambda, printing "Hello, Alice!" to the console.Final Answer:
Hello, Alice! -> Option BQuick Check:
Action<string> prints Hello, Alice! [OK]
- Expecting variable name printed instead of parameter value
- Thinking Action returns a value to print
- Missing that Console.WriteLine outputs text
Action<int> printNumber = (int num) => return num.ToString();
Solution
Step 1: Understand Action delegate return type
Actiondelegates always return void, so their lambda expressions cannot return a value.Step 2: Analyze the lambda expression
The lambda tries to returnnum.ToString(), which is a string, causing a mismatch with void return type.Final Answer:
Lambda expression cannot use return with Action delegate -> Option AQuick Check:
Action = void, no return allowed [OK]
- Trying to return a value from an Action delegate
- Confusing Action with Func delegate
- Ignoring lambda syntax rules
Action delegate that takes three parameters: an int, a string, and a bool. It should print a message only if the bool is true. Which of the following code snippets correctly implements this?Solution
Step 1: Check parameter count and types
Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); }; correctly declares anActionwith three parameters: int, string, and bool. Action printIfTrue = (num, text) => { if(flag) Console.WriteLine($"{num}: {text}"); }; has only two parameters, so it's invalid.Step 2: Verify the lambda body logic
Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); }; uses an if statement to print only if the bool is true, which matches the requirement. Action printIfTrue = (num, text, flag) => { if(flag) return Console.WriteLine($"{num}: {text}"); }; incorrectly uses return with Console.WriteLine, which is invalid for Action. Action printIfTrue = (num, text, flag) => Console.WriteLine($"{num}: {text}"); always prints regardless of the bool.Final Answer:
Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); }; -> Option DQuick Check:
Correct params and conditional print = Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); }; [OK]
- Using return inside Action lambda
- Mismatching parameter count
- Ignoring the bool condition in printing
Ready to prove it?
Pass the test and this pattern moves unknown
