I\'m asking specifically about VB.NET, but I imagine the general principles are the same in other languages. I thought an event was a first-class concept in .NET, but it seems
No, an event is just a combination of two or three methods (the "raise" part is optional) in the same way that a property is a combination of one or two methods.
AddHandler
and RemoveHandler
don't modify methods at all. They just call the "add" and "remove" parts of the event, which are resposible for the implementation part.
Typically an event is implemented via a reference to a field with the appropriate delegate type, with Delegate.Combine and Delegate.Remove used to perform the appropriate operations. (The field value will be changed - bear in mind that delegate types are immutable.) Raising an event just consists of invoking the delegate.
As for why AddHandler
etc are separate statement types - if they were methods, what would the parameters be? Something has to refer to "the event". Basically an AddHandler
statement corresponds to the appropriate event "add" method, just as a property fetch corresponds to the appropriate property "get" method. You can do this with reflection, via EventInfo.AddHandler.
See my article on delegates and events for more details which may help - it's from a C# background, but the principles are obviously the same.