0
0
C Sharp (C#)programming~10 mins

Action delegate type in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
C Sharp (C#)
Action<string> greet = name => Console.WriteLine($"Hello, {name}!");
greet("Alice");
Defines an Action delegate that prints a greeting and then calls it with "Alice".
Execution Table
StepActionEvaluationResult
1Define Action delegate 'greet'Action<string> greet = lambdagreet holds reference to lambda
2Invoke greet with "Alice"greet("Alice")Calls lambda with name="Alice"
3Execute lambda bodyConsole.WriteLine($"Hello, {name}!")Prints: Hello, Alice!
4End of programNo more codeProgram finishes
💡 Program ends after greet is invoked and message printed
Variable Tracker
VariableStartAfter 1After 2Final
greetnulllambda referencelambda referencelambda reference
nameundefinedundefined"Alice"undefined after call
Key Moments - 3 Insights
Why does the Action delegate not return any value?
Action delegates are designed to represent methods that return void, so no value is returned when invoked, as shown in execution_table step 3.
How does the lambda expression relate to the Action delegate?
The lambda expression is assigned to the Action delegate variable 'greet' at step 1, meaning 'greet' holds the method to run when invoked.
What happens to the parameter 'name' after the Action is invoked?
The parameter 'name' exists only during the call (step 3) and is not stored afterward, as seen in variable_tracker where 'name' is undefined before and after invocation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the Action delegate do?
APrints a greeting message
BReturns a string value
CThrows an error
DChanges the value of 'greet'
💡 Hint
Check execution_table row 3 under Result column for what happens during invocation
According to variable_tracker, what is the value of 'greet' after step 1?
Anull
Blambda reference
Cundefined
D"Alice"
💡 Hint
Look at variable_tracker row for 'greet' after 1
At which step does the program finish execution?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
See execution_table exit_note and step 4 description
Concept Snapshot
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
Full Transcript
This example shows how to use the Action delegate type in C#. First, an Action delegate named 'greet' is defined and assigned a lambda expression that takes a string parameter 'name' and prints a greeting. When 'greet' is invoked with the argument "Alice", the lambda runs and prints "Hello, Alice!". The Action delegate does not return any value. Variables like 'greet' hold the method reference, and 'name' exists only during the call. The program ends after the Action is invoked and the message is printed.