views:

112

answers:

1

I was wondering if anyone could tell me the raw code equivalent to the += operator for adding a method to an event. I am curious to how it it works from a technical standpoint.

+9  A: 

An event defines a set of methods including "add" and "remove" (in the same way that a property defines "get" and "set"). to this is effectively:

obj.add_SomeEvent(handler);

Internally, the event could do anything; there are 2 common cases:

  • events with a delegate field (including "field-like" events)
  • EventHandlerList implementations

With a delegate, it effectively uses Delegate.Combine:

handler = Delegate.Combine(handler, value);

With an EventHandlerList there is a key object:

Events.AddHandler(EventKey, value);
Marc Gravell