How can I clear event subscriptions in C#?

前端 未结 10 1909
野趣味
野趣味 2020-11-30 18:43

Take the following C# class:

c1 {
 event EventHandler someEvent;
}

If there are a lot of subscriptions to c1\'s someEven

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 19:07

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

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

提交回复
热议问题