Events: Did You Know?

I am a factoid junky. I derive tremendous joy from exploring the nuts and bolts of languages. For example, did you know that the FrameworkSDK folder (usually at C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Tool Developers Guide\docs) contains the Common Language Infrastructure (CLI) specification. If you want to know exactly what is and is not part of the CLI then the place to check is the specification documents. Sometimes you may be surprised by what you find.

One piece of information that may take some exploration is how events are emitted as Common Intermediate Language (CIL). CIL is the assembly-like code is written by the compiler. CIL exists to permit a just-in-time compiler to perform security checks on code before it is compiled to machine language and run. (There are other benefits here too, see my "Visual Basic .NET Power Coding" for a discussion of the benefits of CIL.) When an event is emitted to CIL two methods are emitted too. The methods are named add_eventname and remove_eventname. For example, an event named MyEvent will cause VB.NET to emit add_MyEvent and remove_MyEvent. These special methods are called event subscription methods. Their purpose is to support adding and removing event handlers from an event type. For instance, when you write AddHandler object.Event, AddressOf EventHandler (where object.Event is an object's event that you want to subscribe to and EventHandler is the subscribing method) then the add method is called. You can verify the existence of the automatically emitted subscription methods by trying to manually introduce these methods (see Listing 1).

Listing 1: A class with an event member.

Public Class HasEvent
  Public Event MyEvent As EventHandler

  Public Sub add_MyEvent(ByVal Value As EventHandler)
    MyEvent = System.Delegate.Combine(MyEvent, Value)
  End Sub

  Public Sub remove_MyEvent(ByVal Value As EventHandler)
    MyEvent = System.Delegate.Remove(MyEvent, Value)
  End Sub

End Class

If you define the preceding class (or a class with similar methods) then you will get a compile error. The error will get two errors that indicate that these methods conflict with two implicitly defined methods (see Figure 1). And, if you check the assembly with the ildasm.exe utility then you can quickly verify that the add and remove subscription events are in fact defined automatically for you (see Figure 2).

Click here for a larger image.

Figure 1: Event subscription methods are added automatically by VB.NET.

Click here for a larger image.

Figure 2: The add and remove subscription methods shown with ildasm.exe, Microsoft's CIL disassembler.

One might ask "why should I care?" Well, C# permits programmers to define their own add and remove subscription events. As a result a C# developer can extend the behavior of the add and remove subscription methods. One useful application of the add and remove handler is to surface an event on a constituent control. Here is what I mean.

Suppose you define a UserControl or custom control that has child controls. Now suppose you want to permit consumers to assign an event handler to an event on that child control. (This is referred to as surfacing constituent properties and events.) If you can implement an add and remove block then you can surface the constituent event; without the add and remove block you have to control a special method and assign the event handler programmatically at runtime rather than using the designer at design time. This has the affect of making it harder to consume the constituent event. Here is what the add and remove block for a C# event my look like.

private event EventHandler myEvent;
public event EventHandler MyEvent
    myEvent += value;
    myEvent -= value;

The event myEvent (case counts in C#) defines the storage for the event. This could be an event on a constituent control. The public event declaration, MyEvent, defines an add and remove block. In the example we are only performing the same work that the default subscription methods would perform, but we could add any code we desired.

I am not sure why subscription methods are not available to VB programmers, but in some situations-like surfacing constituent control events-the technique is especially useful.


The Common Language Specification (CLS) defines the minimum things that a language must support to be compliant. There are optional features too. One such optional feature seems to be whether the programmer can write add and remove subscription blocks for events. The worrisome aspect of compliance is that VB.NET might slowly evolve to have fewer of the cooler features that her brother C# will have.

There are some very cool new language features in C#-like operator overloading-and likely many more cool features to come. Personally and professionally I hope VB.Net is not left out of this bounty. To a degree it is up to VB.NET developers. If we make our voices heard then history won't repeat itself, leaving VB the poor cousin once again.

About the Author

Paul Kimmel is a freelance writer for and Look for his upcoming book Visual Basic .NET Power Coding from Addison-Wesley. Paul Kimmel is available to help design and build your .NET solutions and can be contacted at


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date