C# Events and Thread Safety

前端 未结 15 1144
甜味超标
甜味超标 2020-11-22 06:00

UPDATE

As of C# 6, the answer to this question is:

SomeEvent?.Invoke(this, e);

I frequently hear/read the fo

15条回答
  •  -上瘾入骨i
    2020-11-22 06:29

    I see a lot of people going toward the extension method of doing this ...

    public static class Extensions   
    {   
      public static void Raise(this EventHandler handler, 
        object sender, T args) where T : EventArgs   
      {   
        if (handler != null) handler(sender, args);   
      }   
    }
    

    That gives you nicer syntax to raise the event ...

    MyEvent.Raise( this, new MyEventArgs() );
    

    And also does away with the local copy since it is captured at method call time.

提交回复
热议问题