Rico Suter's blog.
 


C# only supports multicast events and delegates. Even if you define a delegate property without the event keyword, it still remains multicasted:

public class Foo
{
    public Action MyEvent { get; set; }
}

var foo = new Foo();
foo.MyEvent += delegate { }; 
foo.MyEvent += delegate { }; 

The only difference between a regular property with a delegate type and an event property are:

  • The event property can only be invoked by its class, the delegate in the regular property can also be invoked from outside (which may not be desired)
  • Accessing and assigning an event property is only allowed within the class
  • The event property provides add and remove accessors instead of set and get accessors

In the end, the event keyword does only change the way a delegate can be accessed and invoked; the underlying type is the same.

However there is a way to mimic a singlecast event property for the clients of a class:

public class Bar
{
    private Action _myAction;
    public event Action MyAction 
    {
        add { _myAction = value; }
        remove { _myAction -= value; }
    }
}

As you can see, adding a new delegate always removes the existing delegate and thus only one delegate can be registered at one time…

Note: The registered delegates are called in the order they are added and a multicast delegate returns the result of the last delegate invocation if it has a return type (e.g. if the delegate is of type Func<T>). Read more on this MSDN article.



Discussion