How can I clear event subscriptions in C#?

C#.NetEventsDelegates

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

public class c1
{
    event EventHandler someEvent;
    public ResetSubscriptions() => someEvent = null;    
}

Solution 3 - C#

class c1
{
    event EventHandler someEvent;
    ResetSubscriptions() => someEvent = delegate { };
}

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

Solution 4 - C#

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.

Solution 5 - C#

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.
}

Solution 6 - C#

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

Solution 7 - C#

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).

Solution 8 - C#

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;
	}
}

Solution 9 - C#

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.

Solution 10 - C#

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();
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprogrammerView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#programmerView Answer on Stackoverflow
Solution 3 - C#FengView Answer on Stackoverflow
Solution 4 - C#Jonathan C DickinsonView Answer on Stackoverflow
Solution 5 - C#CaryView Answer on Stackoverflow
Solution 6 - C#MicahView Answer on Stackoverflow
Solution 7 - C#umlcatView Answer on Stackoverflow
Solution 8 - C#GoogolView Answer on Stackoverflow
Solution 9 - C#JalalView Answer on Stackoverflow
Solution 10 - C#barthdamonView Answer on Stackoverflow