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

Publisher-subscriber execution model in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Publisher-subscriber execution model
Publisher creates event
Subscribers register handlers
Publisher triggers event
All subscribers receive notification
Subscribers execute their handlers
Event handling complete
The publisher creates an event and subscribers register their handlers. When the publisher triggers the event, all subscribers get notified and run their handlers.
Execution Sample
C Sharp (C#)
using System;

class Publisher {
  public event Action OnChange;
  public void Notify() => OnChange?.Invoke();
}

class Subscriber {
  public void Respond() => Console.WriteLine("Subscriber notified");
}

var pub = new Publisher();
var sub = new Subscriber();
pub.OnChange += sub.Respond;
pub.Notify();
This code shows a publisher with an event and a subscriber that registers a handler. When the publisher notifies, the subscriber prints a message.
Execution Table
StepActionEvent StateSubscribers RegisteredOutput
1Create Publisher instanceNo event triggeredNone
2Create Subscriber instanceNo event triggeredNone
3Subscriber registers Respond handlerNo event triggeredRespond handler added
4Publisher calls Notify()Event triggeredRespond handler addedSubscriber notified
5Event handling completeNo event triggeredRespond handler remains
💡 Notify() finished, all subscribers notified
Variable Tracker
VariableStartAfter Step 3After Step 4Final
pub.OnChangenullRespond handler addedRespond handler addedRespond handler added
subnullSubscriber instanceSubscriber instanceSubscriber instance
Key Moments - 3 Insights
Why does the event call use OnChange?.Invoke() instead of just OnChange()?
The ?. operator checks if any subscribers are registered before calling Invoke. This prevents errors if no handlers exist, as shown in step 4 of the execution_table.
What happens if multiple subscribers register to the same event?
All registered handlers are called in order when the event triggers. The execution_table shows one subscriber, but more would be called similarly.
Does the event keep subscribers after Notify() is called?
Yes, subscribers remain registered until explicitly removed. Step 5 shows the handler still registered after Notify().
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of pub.OnChange after step 3?
ARespond handler added
Bnull (no handlers)
CEvent triggered
DEmpty delegate
💡 Hint
Check the 'Event State' and 'Subscribers Registered' columns at step 3
At which step does the subscriber print 'Subscriber notified'?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table
If no subscriber registers, what happens when Notify() is called?
ANullReferenceException error
BEvent triggers but no output
CNothing happens, safely ignored
DProgram crashes
💡 Hint
Refer to the use of ?.Invoke() in the code and step 4 explanation
Concept Snapshot
Publisher-subscriber model:
- Publisher defines event (e.g., event Action OnChange)
- Subscribers register handlers (+=)
- Publisher triggers event (Invoke())
- All subscribers notified in order
- Use ?.Invoke() to avoid errors if no subscribers
Full Transcript
The publisher-subscriber execution model involves a publisher that creates an event and subscribers that register their handlers to this event. When the publisher triggers the event by calling Invoke, all registered subscribers receive the notification and execute their handlers. The ?.Invoke() syntax ensures safe calling even if no subscribers are registered. Subscribers remain registered until removed. This model allows decoupled communication where publishers do not need to know about subscribers directly.