Tip

Visual Basic team offering Extension Methods tutorials

Visual Basic 9.0 is full of new features. One of them is Extension Methods, which, in the words of Scott Wisniewski of the VB compiler group, "provide a very powerful and elegant way to inject your own custom functionality into other people's types….Extension Methods enable you to create a method in a module decorated with an attribute and have that method appear as if it was an instance method defined on another type."

Wisniewski recently posted a six-part Extension Methods tutorial on Microsoft's Visual Basic Team blog. He focused on this feature of Visual Basic 9.0 because, well, it is his favorite. Below we link to each part and offer a bit of insight into what is covered.

Part 1: An introduction to Extension Methods describes how they provide "discoverability." In Visual Studio 2005, calling a utility method uses a different syntax than calling an instance method, so programmers must know, ahead of time, whether they are calling a shared utility method written by a third party or an instance method provided directly by a class author, Wisniewski notes. Visual Studio Orcas' Extension Methods merge these types of methods.

Part 2: Rules for consuming Extension Methods provides tips for defining and consuming these methods. Wisniewski points out the differences between Extension Methods in C# and in VB. He also outlines the "precedence levels" that determine which Extension Method takes precedence in the event that two methods conflict.

Part 3: Extension Methods and byval / byref parameters offers a quick look at how the compiler evaluates Extension Methods calls and a hint or two for working with value-type extensions.

Part 4: Extension Methods and late binding explains why Extension Methods do not support late binding -- it is because the methods are closely tied to IntelliSense, which does not work in late-bound scenarios. Wisniewski then points out that, as a result, Extension Methods cannot be called off any expression statically typed as Object.

Part 5: Generics and Extension Methods discusses why Visual Studio Orcas deals with generic Extension methods differently than regular generic methods. "When binding against extension methods we now perform generic type parameter inference in two passes instead of one," Wisniewski writes. "During the first pass we infer types for type parameters referenced by the first argument and during the second pass we infer types for any type parameters referenced by subsequent arguments." There are three reasons for this -- transparency, better use of IntelliSense and improved query support.

Part 6: Extension Methods Best Practices gives seven tidbits of advice, such as putting Extension Methods into their own namespaces and being as specific as possible when extending a type.

This was first published in April 2007

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.