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

前端 未结 8 733
梦谈多话
梦谈多话 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题