Concept Flow - Action delegate type
Define Action delegate
Assign method or lambda
Invoke Action
Execute method body
Finish
The Action delegate holds a method reference with no return value and can be invoked to run that method.
Action<string> greet = name => Console.WriteLine($"Hello, {name}!"); greet("Alice");
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Define Action delegate 'greet' | Action<string> greet = lambda | greet holds reference to lambda |
| 2 | Invoke greet with "Alice" | greet("Alice") | Calls lambda with name="Alice" |
| 3 | Execute lambda body | Console.WriteLine($"Hello, {name}!") | Prints: Hello, Alice! |
| 4 | End of program | No more code | Program finishes |
| Variable | Start | After 1 | After 2 | Final |
|---|---|---|---|---|
| greet | null | lambda reference | lambda reference | lambda reference |
| name | undefined | undefined | "Alice" | undefined after call |
Action delegate type: - Represents a method with no return value (void) - Can take parameters (e.g., Action<string>) - Assigned with method or lambda - Invoked like a method - Used for callbacks or simple actions
Action delegate type in C#?Action is a delegate type used to represent methods that do not return a value (void) but can take parameters.Action represents methods returning void and can take parameters. Other options mention return values or asynchronous behavior, which are incorrect.Action delegate that takes two integer parameters?Action delegate syntax for parameters is Action<T1, T2> for two parameters.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.Action<string> greet = name => Console.WriteLine($"Hello, {name}!");
greet("Alice");greet variable is an Action<string> delegate that takes a string and prints a greeting with that string.greet("Alice") runs the lambda, printing "Hello, Alice!" to the console.Action<int> printNumber = (int num) => return num.ToString();
Action delegates always return void, so their lambda expressions cannot return a value.num.ToString(), which is a string, causing a mismatch with void return type.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 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.Pass the test and this pattern moves unknown