Multicast Delegate Events as Properties

The saying goes “you learn something new every day”, and today is no
exception.  I’m not sure how I missed this, but never less, this
post shows how to gain finer control over adding and removing of event
handlers to public multicast delegate events.

Typically, most C# developers are used writing event code as follows:

using System;
public delegate void MyDelegate(); // delegate declaration

public interface I
{
event MyDelegate MyEvent;
void FireAway();
}

public class MyClass: I
{
public event MyDelegate MyEvent;

public void FireAway()
{
if (MyEvent != null)
MyEvent();
}
}

public class MainClass
{
static private void f()
{
Console.WriteLine(“This is called when the event fires.”);
}

static public void Main ()
{
I i = new MyClass();

i.MyEvent += new MyDelegate(f);
i.FireAway();
}
}

The above code exposes a public multicast delegate event, which client callers subscribe to by adding handlers using the +=
operator.  However, it is possible to define a multicast delegate
event as a property, where the developer can control the adding and
removal of event handlers with add and remove accessor
decelerations. 

The example below defines properties for each event, and the accessor
decelerations on each property manages the storage of event handlers
using a private hash table. This approach is usually undertaken when a
class defines many events, where the developer expects most of the
events to be unimplemented, thus saving some overhead in creating
multicast delegate event instances that are never assigned a handler.

using System;
using System.Collections;

public delegate void MyDelegate1(int i);
public delegate void MyDelegate2(string s);
public delegate void MyDelegate3(int i, object o);
public delegate void MyDelegate4();

public class PropertyEventsSample
{
private Hashtable eventTable = new Hashtable();

public event MyDelegate1 Event1
{
add
{
eventTable[“Event1”] = (MyDelegate1)eventTable[“Event1”] + value;
}
remove
{
eventTable[“Event1”] = (MyDelegate1)eventTable[“Event1”] – value;
}
}

public event MyDelegate1 Event2
{
add
{
eventTable[“Event2”] = (MyDelegate1)eventTable[“Event2”] + value;
}
remove
{
eventTable[“Event2”] = (MyDelegate1)eventTable[“Event2”] – value;
}
}

public event MyDelegate2 Event3
{
add
{
eventTable[“Event3”] = (MyDelegate2)eventTable[“Event3”] + value;
}
remove
{
eventTable[“Event3”] = (MyDelegate2)eventTable[“Event3”] – value;
}
}

public event MyDelegate3 Event4
{
add
{
eventTable[“Event4”] = (MyDelegate3)eventTable[“Event4”] + value;
}
remove
{
eventTable[“Event4”] = (MyDelegate3)eventTable[“Event4”] – value;
}
}

public event MyDelegate3 Event5
{
add
{
eventTable[“Event5”] = (MyDelegate3)eventTable[“Event5”] + value;
}
remove
{
eventTable[“Event5”] = (MyDelegate3)eventTable[“Event5”] – value;
}
}

public event MyDelegate4 Event6
{
add
{
eventTable[“Event6”] = (MyDelegate4)eventTable[“Event6”] + value;
}
remove
{
eventTable[“Event6”] = (MyDelegate4)eventTable[“Event6”] – value;
}
}
}