Forwarding events in C#

Absolutely:

class B
{
    private A m_a = new A();

    public event EventType EventB
    {
        add { m_a.EventA += value; }
        remove { m_a.EventA -= value; }
    }
}

In other words, the EventB subscription/unsubscription code just passes the subscription/unsubscription requests on to EventA.

Note that this doesn’t allow you to raise the event just for subscribers who subscribed to EventB, however. It’s like passing someone’s address directly onto a mass marketing company, whereas your original way is more like subscribing to the mass marketing company yourself, and allowing people to ask you to send copies of the mails to them.

Leave a Comment