Extension Methods in VB.NET 2008

One of the cool new features of .NET 3.5 is extension methods. What is an extension method? In layman’s terms it's a method that can be added to an existing class without inheriting that existing class. The official definition from Microsoft is "Visual Basic 2008 introduces extension methods, which enable developers to add custom functionality to data types that are already defined without creating a new derived type. Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type."

A derived example is checking if a string is an valid email address. Before extension methods we would create a method similar to this:

Public
Function IsValidEmail(ByVal value As String) As Boolean
     Dim regEx As New System.Text.RegularExpressions.Regex("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$")
     Return regEx.IsMatch(value)
End Function

Calling this method would look something like this:

Dim
email As String = "test@test.com"
If (IsValidEmail("test@test.com")) Then
   
...
End If

With an extension method we can "extend" the string class to include the IsValidEmail. The following is what an extension method looks like in VB.NET:

<Extension()> _
Public Function IsValidEmail(ByVal e As String) As Boolean
   
Dim regEx As New System.Text.RegularExpressions.Regex("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$")
   
Return regEx.IsMatch(e)
End Function

So let's explain what's going on. Before we continue, ALL EXTENSION METHODS MUST BE DEFINED IN A MODULE IN VB.NET. Not sure why that is, but that's the way it is.

The Extension attribute is defined in the System.Runtime.CompilerServices class. The System.Runtime.CompilerServices class must imported into the module defining the extension methods.

The first parameter defined in an extension method is the type of object being extended. In our case "ByVal e As String" is telling the compiler to extend the string class. The first parameter is also the instance object of the class being extended. If you look at the example above the parameter e is being used on the line of code "Return regEx.IsMatch(e)". In the code example below the "e" parameter of our extension method would have the value of the "email" variable which is "test@test.com". 

Dim email As String = "test@test.com"

If
email.IsValidEmail() Then
   
...
End If

Another nice feature is VS 2008 will automatically detect the extension method and provide full intellisense.

Until next time.

Technorati Tags: ,,.NET 3.5

posted @ Tuesday, January 15, 2008 10:32 AM

Print

Comments on this entry:

# re: Extension Methods in VB.NET 2008

Left by JT at 3/27/2008 5:13 PM
Gravatar
Very nice explanation, thank you.

# Using Generics in an Extension Method

Left by Rocks Thoughts at 4/2/2008 10:40 PM
Gravatar
Using Generics in an Extension Method

Your comment:



 (will not be displayed)


 
 
 
Please add 6 and 2 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910