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

前端 未结 8 718
梦谈多话
梦谈多话 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:43

    You can always declare your events like this (not that i recommend it):

    public event EventHandler OnClicked = delegate { };
    

    That way they have something assigned to them when you call them, so they don't throw a null pointer exception.

    You can probably get rid of the delegate keyword in C# 3.0...

提交回复
热议问题