C# Events and Lambdas, alternative to null check?

Posted by Eugarps on Stack Overflow See other posts from Stack Overflow or by Eugarps
Published on 2010-05-20T21:01:50Z Indexed on 2010/05/20 21:10 UTC
Read the original article Hit count: 218

Filed under:

Does anybody see any drawbacks? It should be noted that you can't remove anonymous methods from an event delegate list, I'm aware of that (actually that was the conceptual motivation for this).

The goal here is an alternative to:

if (onFoo != null) onFoo.Invoke(this, null);

And the code:

public delegate void FooDelegate(object sender, EventArgs e);

public class EventTest
{
    public EventTest()
    {
        onFoo += (p,q) => { };
    }

    public FireFoo()
    {
         onFoo.Invoke(this, null);
    }

    public event FooDelegate onFoo;

}

© Stack Overflow or respective owner

Related posts about c#