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

Delegates as callback pattern in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Delegates as callback pattern
Define delegate type
Create method matching delegate
Create method accepting delegate
Call method with delegate as argument
Inside method, invoke delegate (callback)
Callback method runs
Return to caller
This flow shows how a delegate is defined, passed as a callback, and invoked inside another method.
Execution Sample
C Sharp (C#)
delegate void Callback(string message);

void Process(Callback callback) {
    callback("Hello from callback!");
}

void ShowMessage(string msg) {
    Console.WriteLine(msg);
}

Process(ShowMessage);
This code defines a delegate, a method matching it, and passes it as a callback to another method which invokes it.
Execution Table
StepActionEvaluationResult
1Define delegate type 'Callback'delegate void Callback(string)Delegate type created
2Define method 'ShowMessage' matching delegatevoid ShowMessage(string)Method ready
3Define method 'Process' accepting Callback delegatevoid Process(Callback)Method ready
4Call Process with ShowMessage as argumentProcess(ShowMessage)Process starts
5Inside Process, invoke callback with messagecallback("Hello from callback!")ShowMessage called
6ShowMessage prints messageConsole.WriteLine("Hello from callback!")Output: Hello from callback!
7Process returnsEnd of ProcessBack to caller
💡 Process method finishes after invoking callback once
Variable Tracker
VariableStartAfter Step 4After Step 5Final
callbacknullShowMessage method referenceShowMessage method referencenull (after Process ends)
messageN/AN/A"Hello from callback!"N/A
Key Moments - 3 Insights
Why do we pass 'ShowMessage' without parentheses when calling Process?
Because we pass the method itself as a delegate, not calling it immediately. See execution_table step 4 where 'ShowMessage' is passed as a reference.
How does 'Process' know which method to call?
It uses the delegate parameter 'callback' which holds the reference to 'ShowMessage'. This is shown in execution_table step 5 where 'callback' is invoked.
What happens if we call 'callback' multiple times inside 'Process'?
The callback method runs each time it's invoked. The example shows one call at step 5, but more calls would repeat the output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the value passed to the callback?
A"Hello from callback!"
B"ShowMessage"
Cnull
D"Process"
💡 Hint
Check the 'Evaluation' column at step 5 in the execution_table.
At which step does the actual message get printed to the console?
AStep 5
BStep 6
CStep 4
DStep 7
💡 Hint
Look for the step where Console.WriteLine is called in the execution_table.
If we passed 'ShowMessage()' (with parentheses) to Process instead of 'ShowMessage', what would happen?
ACompilation error because of wrong delegate type
BProcess would receive a null delegate
CThe callback would be called immediately before Process runs
DProcess would call ShowMessage twice
💡 Hint
Passing 'ShowMessage()' calls the method immediately, not passing the method reference (see key_moments question 1).
Concept Snapshot
Delegate as callback pattern in C#:
- Define a delegate type matching the callback signature.
- Create a method matching that delegate.
- Pass the method as a delegate argument to another method.
- Inside that method, invoke the delegate to run the callback.
- Allows flexible code by passing behavior as parameters.
Full Transcript
This example shows how delegates work as callbacks in C#. First, a delegate type named Callback is defined to represent methods that take a string and return void. Then, a method ShowMessage matching this delegate is created. Another method Process accepts a Callback delegate as a parameter. When Process is called with ShowMessage as argument, it stores the method reference in the delegate parameter. Inside Process, the delegate is invoked with a message string. This calls ShowMessage, which prints the message to the console. The execution table traces each step: defining delegate, defining methods, calling Process, invoking callback, printing output, and returning. The variable tracker shows how the delegate variable holds the method reference during execution. Key moments clarify why we pass method names without parentheses and how the callback is invoked. The visual quiz tests understanding of the message passed, when printing happens, and what happens if parentheses are used when passing the method. This pattern lets us pass behavior as parameters, making code flexible and reusable.