Is calling an extension method on a “null” reference (i.e. event with no subscribers) evil?

前端 未结 8 715
梦谈多话
梦谈多话 2020-12-12 16:05

Evil or not evil?

public static void Raise(this EventHandler handler, object sender, EventArgs args)
{
   if (handler != null)
   {
      handler(sender, arg         


        
相关标签:
8条回答
  • 2020-12-12 16:53

    Why would it be evil?

    Its purpose is clear: It raises the MyButtonClicked event.

    It does add a function call overhead, but in .NET it will either be optimized away or pretty fast anyway.

    It is slightly trivial, but it fixes my biggest complaint with C#.

    On the whole, I think it's a fantastic idea, and will probably steal it.

    0 讨论(0)
  • 2020-12-12 16:53

    Throwing an exception when there are no handlers is not really preferable by the most. If it does not have an handlers it is better to be empty rather than null.

    0 讨论(0)
提交回复
热议问题