An Action delegate is a predefined delegate type in C# that represents a method that returns void and can take zero or more input parameters.
Click to reveal answer
beginner
How many parameters can an Action delegate have?
An Action delegate can have from zero up to 16 input parameters, but it always returns void (no return value).
Click to reveal answer
beginner
Write a simple example of an Action delegate that takes no parameters.
Action greet = () => Console.WriteLine("Hello!");
greet(); // Prints "Hello!"
Click to reveal answer
intermediate
What is the difference between Action and Func delegates?
Action delegates always return void, while Func delegates return a value and can have input parameters.
Click to reveal answer
beginner
How do you declare an Action delegate that takes two integers and prints their sum?
Action printSum = (a, b) => Console.WriteLine(a + b);
printSum(3, 4); // Prints 7
Click to reveal answer
What does an Action delegate return?
Avoid
Bint
Cstring
Dbool
✗ Incorrect
Action delegates always return void, meaning they do not return any value.
Which of the following is a valid Action delegate declaration?
AAction<int> myFunc;
BFunc<void> myAction;
CFunc<int, string> myAction;
DAction<int, string> myAction;
✗ Incorrect
Action is valid because Action can take parameters and returns void.
How many parameters can an Action delegate have at most?
A16
B8
C4
DNo limit
✗ Incorrect
Action delegates can have up to 16 input parameters.
Which delegate type should you use if you want a method that returns a value?
AAction
BEventHandler
CFunc
DPredicate
✗ Incorrect
Func delegates return a value, unlike Action which returns void.
What is the output of this code?
Action<string> print = s => Console.WriteLine(s);
print("Hi");
Aprint
BHi
Cs
DNo output
✗ Incorrect
The Action delegate prints the string passed to it, so it outputs "Hi".
Explain what an Action delegate is and how it differs from a Func delegate.
Think about return types and input parameters.
You got /4 concepts.
Write a simple example of an Action delegate that takes two integers and prints their product.
Use Action<int, int> and a lambda that multiplies and prints.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the Action delegate type in C#?
easy
A. To represent methods that return void and can take parameters
B. To represent methods that return a value
C. To represent asynchronous methods only
D. To represent methods that take no parameters and return a value
Solution
Step 1: Understand what Action represents
Action is 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 that Action represents 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 A
Quick Check:
Action = void methods with parameters [OK]
Hint: Action always means void return with optional parameters [OK]
Common Mistakes:
Confusing Action with Func which returns a value
Thinking Action is only for methods without parameters
Assuming Action is for async methods only
2. Which of the following is the correct syntax to declare an Action delegate that takes two integer parameters?
easy
A. Action myAction(int, int);
B. Action<int> myAction;
C. Action myAction;
D. Action myAction() => void;
Solution
Step 1: Recall Action delegate syntax
The Action delegate syntax for parameters is Action<T1, T2> for two parameters.
Step 2: Analyze each option
Action myAction; correctly declares an Action with 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 C
Quick Check:
Two parameters use Action<int, int> [OK]
Hint: Use Action for two parameters [OK]
Common Mistakes:
Using parentheses like a method instead of generic syntax
Declaring return type with Action (it always returns void)
Confusing Action with Func syntax
3. What will be the output of the following C# code?
Action<string> greet = name => Console.WriteLine($"Hello, {name}!");
greet("Alice");
medium
A. Hello, name!
B. Hello, Alice!
C. greet
D. No output
Solution
Step 1: Understand the Action delegate usage
The greet variable is an Action<string> delegate that takes a string and prints a greeting with that string.
Step 2: Trace the method call
Calling greet("Alice") runs the lambda, printing "Hello, Alice!" to the console.
Final Answer:
Hello, Alice! -> Option B
Quick Check:
Action<string> prints Hello, Alice! [OK]
Hint: Action runs the method body with given parameters [OK]
Common Mistakes:
Expecting variable name printed instead of parameter value
Thinking Action returns a value to print
Missing that Console.WriteLine outputs text
4. Identify the error in the following code snippet:
A. Lambda expression cannot use return with Action delegate
B. Action delegate cannot have parameters
C. Missing semicolon after lambda
D. Action delegate must return a string
Solution
Step 1: Understand Action delegate return type
Action delegates always return void, so their lambda expressions cannot return a value.
Step 2: Analyze the lambda expression
The lambda tries to return num.ToString(), which is a string, causing a mismatch with void return type.
Final Answer:
Lambda expression cannot use return with Action delegate -> Option A
Quick Check:
Action = void, no return allowed [OK]
Hint: Action lambdas must not return values [OK]
Common Mistakes:
Trying to return a value from an Action delegate
Confusing Action with Func delegate
Ignoring lambda syntax rules
5. You want to create an 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?
Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); }; correctly declares an Action with 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.