How can I clear event subscriptions in C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, events maintain an invocation list of subscribed delegate handlers. Clearing all subscriptions is necessary to prevent memory leaks (publisher keeping subscribers alive), avoid duplicate handler invocations after re-subscription, and reset state during testing. Inside the declaring class, you can set the event to null to remove all handlers. Outside the class, you can only use -= to remove specific handlers. There is no built-in way to clear all event subscriptions from external code without using reflection.
How Events Store Subscriptions
Clearing All Subscriptions from Inside the Class
Removing Specific Handlers with -=
Anonymous lambdas cannot be unsubscribed because you do not have a reference to the original delegate instance:
Pattern: Store Handler Reference for Later Removal
Clearing Events via Reflection (External Code)
When you do not own the class and need to clear its events (e.g., in testing):
This is fragile and should only be used in testing. The backing field name may differ from the event name in some implementations.
Weak Event Pattern (Preventing Memory Leaks)
Common Pitfalls
- Anonymous lambdas cannot be unsubscribed: Subscribing with
event += (s, e) => ...creates a delegate you have no reference to. Calling-=with an identical lambda creates a new delegate instance that does not match the original. Always store the delegate in a variable if you need to unsubscribe later. - Forgetting to unsubscribe causes memory leaks: If the event publisher has a longer lifetime than the subscriber, the subscription keeps the subscriber alive via the delegate reference. The garbage collector cannot collect the subscriber. Unsubscribe in
Dispose()or use the weak event pattern. - Setting event to null from outside the class:
button.Click = nullis a compile error. Theeventkeyword restricts external code to+=and-=only. Only code inside the declaring class can assignnullto the event. Provide aClearHandlers()method if external clearing is needed. - Double-subscribing the same handler: Calling
event += handlertwice adds the handler to the invocation list twice — it will be called twice per event raise. Either check before subscribing or unsubscribe first:event -= handler; event += handler;. - Thread safety when clearing events: Setting an event to
nullwhile another thread is raising it can cause aNullReferenceException. Use theevent?.Invoke()pattern and consider locking if events are raised from multiple threads.
Summary
- Inside the declaring class, set the event to
nullto clear all subscriptions - Outside the class, use
-=with a stored delegate reference to remove specific handlers - Anonymous lambdas cannot be unsubscribed — always store the delegate reference
- Provide a
ClearHandlers()method on your class if external clearing is needed - Unsubscribe in
Dispose()to prevent memory leaks from long-lived publishers
Related reading
- How can I compare directory paths in C?
- How can I compile a .NET application to native code?
- How can I confirm if an async EF6 await db.SaveChangesAsync worked as expected?
- How can I convert a JSON object to a custom C object?
- How can I convert Assembly.CodeBase into a filesystem path in C?
- How can I convert comma separated string into a Listint
- How can I Convert HTML to Text in C?
- How can I convert this foreach code to Parallel.ForEach?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.