BT

Partial Methods in VB and C#

| by Jonathan Allen Follow 575 Followers on Apr 11, 2007. Estimated reading time: 2 minutes |

Two language features, Dynamic Interfaces and Dynamic Identifiers, were cut from VB 9. New features that are being added in their place include Partial Methods. While partial methods share many of the same use cases as events, they have very different implementations.

The VB Team writes,

In a nut-shell, partial methods are a light-weight replacement for events designed primarily for use by automatic code generators. They are declared by creating a private method with an empty body and decorating it with the Partial keyword. The method may then be "re-implemented" elsewhere within its containing class. If the method is implemented, then the compiler will redirect all calls to the partial method to the implementing method. If the method is not implemented in its containing class, then the compiler silently removes any calls to it from the program.

Compared to events, partial methods have a lot of limitations. Unlike an event, which can have multiple handlers, partial methods are limited to a single implementation. And while the implementation can be in a separate partial file, it still must be in the same class. Finally, there are not any runtime equivalents to the ability to add and remove handlers at runtime.

So why use partial methods? Because they are much faster. Cycles must be burned raising an event, even no handlers are actually attached. If they are attached, a delegate must be invoked for each handler.

Partial methods, if they are implemented, always use a faster non-virtual call. If they are not implemented, the compiler completely eliminates any code calling the method. When used with a code generator that exposes a lot of hooks, the potential performance gains are significant. According to Scott Wisniewski, Software Design Engineer of the VB Core Compiler Team,

We needed to add Partial Methods in order to support the DLinq Designer. Some of our earlier CTP’s utilized events to make the properties generated for DLINQ objects customizable. However, the DLinq team did some performance investigations of this and found the performance overhead of events in those scenarios to be unacceptable. As a result, we added partial methods to enable DLINQ to be customizable without hurting performance.

Earlier we mentioned that Dynamic Interfaces and Dynamic Identifiers were cut. These features are not necessarily dead, though they will not be making the Orcas release. Scott Wisniewski writes,

We cut Dynamic Interfaces mainly for resource reasons. In particular, the amount of time we were given to implement Orcas did not enable us to implement all of the features we had originally planned. As a result, we needed to sit down and figure out which features we would be able to implement and which ones we would not. Unfortunately, because Dynamic Interfaces were not necessary for enabling the core LINQ scenarios we ended up cutting them. Currently we are still evaluating which features we want to implement in the version of Visual Studio following Orcas.

Users can submit suggests for the post-Orcas release to by logging a suggestion on the Microsoft Connect site or through the VB Team Blog

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

In time we will arive to C++ separate class definition in header files... by luca barba

...?....

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT