152

Take the following C# class:

c1 {
 event EventHandler someEvent;
}

If there are a lot of subscriptions to c1's someEvent event and I want to clear them all, what is the best way to achieve this? Also consider that subscriptions to this event could be/are lambdas/anonymous delegates.

Currently my solution is to add a ResetSubscriptions() method to c1 that sets someEvent to null. I don't know if this has any unseen consequences.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
programmer
  • 4,342
  • 4
  • 24
  • 21
  • I described a working answer using Reflection here: https://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-an-event/66956934#66956934 – Vassili Apr 05 '21 at 17:23

10 Answers10

192

From within the class, you can set the (hidden) variable to null. A null reference is the canonical way of representing an empty invocation list, effectively.

From outside the class, you can't do this - events basically expose "subscribe" and "unsubscribe" and that's it.

It's worth being aware of what field-like events are actually doing - they're creating a variable and an event at the same time. Within the class, you end up referencing the variable. From outside, you reference the event.

See my article on events and delegates for more information.

David Dowdle
  • 423
  • 6
  • 10
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    If you're stubborn, you can force it clear via reflection. See http://stackoverflow.com/questions/91778/how-to-remove-all-event-handlers-from-a-control/91853#91853 . – Brian Oct 29 '10 at 21:36
  • 1
    @Brian: It depends on the implementation. If it's *just* a field-like event or an `EventHandlerList`, you may be able to. You'd have to recognise those two cases though - and there could be any number of other implementations. – Jon Skeet Oct 30 '10 at 06:49
  • @Joshua: No, it will set the variable to have a value of null. I agree that the variable won't be called `hidden`. – Jon Skeet May 11 '16 at 17:37
  • @JonSkeet That's what I (thought) I said. The way it was written confused me for 5 minutes. –  May 11 '16 at 18:01
  • @JoshuaLamusga: Well you said it would clear an invocation list, which sounds like modifying an existing object. – Jon Skeet May 11 '16 at 18:01
38

Add a method to c1 that will set 'someEvent' to null.

public class c1
{
    event EventHandler someEvent;
    public ResetSubscriptions() => someEvent = null;    
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
programmer
  • 4,342
  • 4
  • 24
  • 21
10
class c1
{
    event EventHandler someEvent;
    ResetSubscriptions() => someEvent = delegate { };
}

It is better to use delegate { } than null to avoid the null ref exception.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Feng
  • 109
  • 1
  • 2
7

The best practice to clear all subscribers is to set the someEvent to null by adding another public method if you want to expose this functionality to outside. This has no unseen consequences. The precondition is to remember to declare SomeEvent with the keyword 'event'.

Please see the book - C# 4.0 in the nutshell, page 125.

Some one here proposed to use Delegate.RemoveAll method. If you use it, the sample code could follow the below form. But it is really stupid. Why not just SomeEvent=null inside the ClearSubscribers() function?

public void ClearSubscribers ()
{
   SomeEvent = (EventHandler) Delegate.RemoveAll(SomeEvent, SomeEvent);
   // Then you will find SomeEvent is set to null.
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Cary
  • 372
  • 1
  • 5
  • 14
  • Delegate.RemoveAll valid for MulticastDelegate: `public delegate string TableNameMapperDelegate(Type type);public static TableNameMapperDelegate TableNameMapper;` ? – Kiquenet Aug 05 '20 at 09:29
6

Setting the event to null inside the class works. When you dispose a class you should always set the event to null, the GC has problems with events and may not clean up the disposed class if it has dangling events.

Jonathan C Dickinson
  • 7,181
  • 4
  • 35
  • 46
5

You can achieve this by using the Delegate.Remove or Delegate.RemoveAll methods.

Micah
  • 111,873
  • 86
  • 233
  • 325
3

Conceptual extended boring comment.

I rather use the word "event handler" instead of "event" or "delegate". And used the word "event" for other stuff. In some programming languages (VB.NET, Object Pascal, Objective-C), "event" is called a "message" or "signal", and even have a "message" keyword, and specific sugar syntax.

const
  WM_Paint = 998;  // <-- "question" can be done by several talkers
  WM_Clear = 546;

type
  MyWindowClass = class(Window)
    procedure NotEventHandlerMethod_1;
    procedure NotEventHandlerMethod_17;

    procedure DoPaintEventHandler; message WM_Paint; // <-- "answer" by this listener
    procedure DoClearEventHandler; message WM_Clear;
  end;

And, in order to respond to that "message", a "event handler" respond, whether is a single delegate or multiple delegates.

Summary: "Event" is the "question", "event handler (s)" are the answer (s).

umlcat
  • 4,091
  • 3
  • 19
  • 29
1

Remove all events, assume the event is an "Action" type:

Delegate[] dary = TermCheckScore.GetInvocationList();

if ( dary != null )
{
    foreach ( Delegate del in dary )
    {
        TermCheckScore -= ( Action ) del;
    }
}
Googol
  • 39
  • 1
  • 1
    If you're inside of the type that declared the event you don't need to do this, you can just set it to null, if you're outside of the type then you can't get the invocation list of the delegate. Also, your code throws an exception if the event is null, when calling `GetInvocationList`. – Servy Dec 20 '13 at 20:03
0

This is my solution:

public class Foo : IDisposable
{
    private event EventHandler _statusChanged;
    public event EventHandler StatusChanged
    {
        add
        {
            _statusChanged += value;
        }
        remove
        {
            _statusChanged -= value;
        }
    }

    public void Dispose()
    {
        _statusChanged = null;
    }
}

You need to call Dispose() or use using(new Foo()){/*...*/} pattern to unsubscribe all members of invocation list.

Jalal
  • 6,594
  • 9
  • 63
  • 100
-1

Instead of adding and removing callbacks manually and having a bunch of delegate types declared everywhere:

// The hard way
public delegate void ObjectCallback(ObjectType broadcaster);

public class Object
{
    public event ObjectCallback m_ObjectCallback;
    
    void SetupListener()
    {
        ObjectCallback callback = null;
        callback = (ObjectType broadcaster) =>
        {
            // one time logic here
            broadcaster.m_ObjectCallback -= callback;
        };
        m_ObjectCallback += callback;

    }
    
    void BroadcastEvent()
    {
        m_ObjectCallback?.Invoke(this);
    }
}

You could try this generic approach:

public class Object
{
    public Broadcast<Object> m_EventToBroadcast = new Broadcast<Object>();

    void SetupListener()
    {
        m_EventToBroadcast.SubscribeOnce((ObjectType broadcaster) => {
            // one time logic here
        });
    }

    ~Object()
    {
        m_EventToBroadcast.Dispose();
        m_EventToBroadcast = null;
    }

    void BroadcastEvent()
    {
        m_EventToBroadcast.Broadcast(this);
    }
}


public delegate void ObjectDelegate<T>(T broadcaster);
public class Broadcast<T> : IDisposable
{
    private event ObjectDelegate<T> m_Event;
    private List<ObjectDelegate<T>> m_SingleSubscribers = new List<ObjectDelegate<T>>();

    ~Broadcast()
    {
        Dispose();
    }

    public void Dispose()
    {
        Clear();
        System.GC.SuppressFinalize(this);
    }

    public void Clear()
    {
        m_SingleSubscribers.Clear();
        m_Event = delegate { };
    }

    // add a one shot to this delegate that is removed after first broadcast
    public void SubscribeOnce(ObjectDelegate<T> del)
    {
        m_Event += del;
        m_SingleSubscribers.Add(del);
    }

    // add a recurring delegate that gets called each time
    public void Subscribe(ObjectDelegate<T> del)
    {
        m_Event += del;
    }

    public void Unsubscribe(ObjectDelegate<T> del)
    {
        m_Event -= del;
    }

    public void Broadcast(T broadcaster)
    {
        m_Event?.Invoke(broadcaster);
        for (int i = 0; i < m_SingleSubscribers.Count; ++i)
        {
            Unsubscribe(m_SingleSubscribers[i]);
        }
        m_SingleSubscribers.Clear();
    }
}
barthdamon
  • 39
  • 4