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

Event unsubscription and memory in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event unsubscription and memory
Subscribe event handler
Event raised
Handler called
Unsubscribe event handler
Event raised again
Handler NOT called
Memory freed if no other refs
This flow shows subscribing an event handler, calling it when event fires, then unsubscribing so it no longer runs and memory can be freed.
Execution Sample
C Sharp (C#)
using System;

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

var pub = new Publisher();
void Handler() => Console.WriteLine("Called");
pub.OnChange += Handler;
pub.Raise();
pub.OnChange -= Handler;
pub.Raise();
This code subscribes a handler to an event, raises the event (handler runs), unsubscribes, then raises again (handler does not run).
Execution Table
StepActionEvent Handler ListOutput
1Create Publisher instanceempty
2Define Handler methodempty
3Subscribe Handler to OnChangeHandler
4Raise event (pub.Raise())HandlerCalled
5Unsubscribe Handler from OnChangeempty
6Raise event again (pub.Raise())empty
💡 Event handler list empty after unsubscription, so no output on second raise.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
pub.OnChangenullHandlernullnull
Key Moments - 2 Insights
Why does the handler not run after unsubscription?
Because in step 5 the handler is removed from the event list, so at step 6 the event has no handlers to call (see execution_table rows 5 and 6).
How does unsubscription help with memory?
If the handler is not unsubscribed, the event publisher keeps a reference to it, preventing garbage collection. Unsubscribing removes this reference, allowing memory to be freed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the event handler list after step 3?
Aempty
Bnull
CHandler
DMultiple handlers
💡 Hint
Check the 'Event Handler List' column at step 3 in execution_table.
At which step does the event handler get removed?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look at the 'Action' column in execution_table for when unsubscription happens.
If we did not unsubscribe the handler, what would happen at step 6?
AHandler runs again
BNo output
CError thrown
DEvent handler list becomes empty
💡 Hint
Refer to the output column at step 4 and consider what happens if handler remains subscribed.
Concept Snapshot
Event subscription adds handlers to a list.
Raising event calls all handlers.
Unsubscription removes handlers.
Without unsubscription, handlers stay referenced.
This can cause memory leaks.
Always unsubscribe to free memory.
Full Transcript
This example shows how subscribing an event handler adds it to the event's internal list. When the event is raised, all subscribed handlers run. Unsubscribing removes the handler from the list, so it no longer runs when the event fires. This also allows the handler to be garbage collected, preventing memory leaks. The execution table traces each step: creating the publisher, subscribing the handler, raising the event (handler runs), unsubscribing, and raising again (no handler runs). The variable tracker shows the event handler list changing from null to containing the handler and back to null. Key moments clarify why unsubscription stops the handler from running and why it is important for memory management. The quiz tests understanding of the event handler list state and effects of unsubscription.