Note: This article is based on Orcas Beta 2 (Visual Studio 2008).
Visual Basic 2008 extension methods enable you to "add" functionality to existing types without creating a new derived type.
Types That Can Be Extended
· Classes (reference types)
· Structures (value types)
· ByRef and ByVal arguments
· Generic method parameters
Extension methods make it possible to create a method in a Visual Basic module that can be called as if it were an instance method of another type.
An extension method can be a Sub procedure or a Function procedure. You cannot define an extension property, field, or event. All extension methods must be marked with the extension attribute from the System.Runtime.CompilerServices namespace.
The first parameter in an extension method definition specifies which data type the method extends. When the method is run, the first parameter is bound to the instance of the data type that invokes the method.
In the following example an extension method is defined for the .NET String type.
Example – Add ‘Display’ extension method to .NET String type
The following code defines a Display extension method for the .NET String data type. The method uses MessageBox.Display to display a string. The type of the aString parameter of the Display method, establishes that the method extends the String class.
' You must define type extensions in a Visual Basic module.
'All extension methods must be marked with the extension attribute
' from the System.Runtime.CompilerServices namespace.
Public Sub Display(ByVal aString As String)
The following code, in a button click handler in a Windows form, calls the String ‘Display’ extension method defined above.
Public Class Form1
Private Sub DisplayStringButton_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Dim name As String = "John Doe"
The name 'John Doe' is displayed in a message box.
You can use extension methods to extend a type’s methods, but not to override them.
All that is required to be able to run extension methods is that they be in scope. If an extension method is in scope, it is visible in IntelliSense and can be called as if it were an ordinary instance method.
Notice that when the ‘Display’ extension method is invoked in example, no argument is sent in for the first parameter. Parameter aString in the extension method definition is automatically bound to the instance of String that calls them.
Because the first parameter specifies the data type that the extension method extends, it is required and cannot be optional. For that reason, Optional parameters and ParamArray parameters are excluded.
When an extension method has the same name as an existing method but a different signature, both methods are allowed as long as the signatures do not conflict.
When an extension method has the same name and signature as an existing method in the type being extended, resolve ambiguity by using the fully qualified name to specify the extension method you are calling. In the example the Display extension method is defined in a module named ExampleTypeExtensions, the fully qualified name is ExampleTypeExtensions.Display.
In general, it is recommended that you implement extension methods sparingly. When possible, client code that extends an existing type should do so by creating a new type derived from the existing type.
When using an extension method to extend a type whose source code you cannot change, you run the risk that a change in the implementation of the type will cause your extension method to break.