Freigeben über


Extension Methods

[Table of Contents] [Next Topic]

Extension methods are special methods that, while they are not part of a data type, you can call them as though they were part of the data type.  Extension methods are a feature of VB 9.0.

Writing Extension Methods

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOCTo write an extension method, you write a function in a module, and precede it with an attribute.  The first argument to this function needs to be of the type that the extension method operates on.

Note that the following class does not define a PrintIt method:

Public Class SampleClass
Public IntField As Integer
Sub New(ByVal arg As Integer)
IntField = arg
End Sub
End Class

Module Module1
<System.Runtime.CompilerServices.Extension()> _
Private Sub PrintIt(ByVal arg As SampleClass)
Console.WriteLine("IntField:{0}", arg.IntField)
End Sub
Sub Main()
Dim sampleClass As New SampleClass(10)
sampleClass.PrintIt()
End Sub
End Module

Notice that you can call the PrintIt method as though it were part of the class.

When you run this code, it outputs:

IntField:10

But the really cool thing is that you can write extension methods for a type that in and of itself, can't contain methods.  The most important example of this is that you can write extension methods for interfaces.  You could also write an extension method for an abstract class.

You can define extension methods for parameterized types as well as non-parameterized types.  The standard query operators are almost all extension methods that are defined on IEnumerable(Of T).

Note that you can define your own extension methods for IEnumerable(Of T).  When there isn't a standard query operator that does exactly what you want, you can write your own extension method.  This is a powerful technique that adds expressiveness to your code.

When you are writing pure functional code, extension methods are important.  There are times that writing extension methods on IEnumerable(Of T) is the way that you want do things.  We'll be using this technique sometimes when writing FP code.

Extension Methods are Vital for LINQ

Extension methods are an integral part of LINQ.  Consider the following code that contains a LINQ query expression:

Dim source As Integer() = { 3, 6, 4, 8, 9, 5, 3, 1, 7, 0 }

Dim query As IEnumerable(Of Integer) = _
From i In source _
where i > 5 _
Select i * 2

For Each i In query
Console.WriteLine(i)
Next

This code is functionally identical to this:

Dim source As Integer() = { 3, 6, 4, 8, 9, 5, 3, 1, 7, 0 }

Dim query As IEnumerable(Of Integer) = _
source.Where(Function (i) i > 5) _
.Select(Function (i) i * 2)

For Each i In query
Console.WriteLine(i)
Next

In fact, you can see that there is a direct translation from the query expression to the same query that is expressed in method notation.  I believe that in early versions of the VB 9.0 compiler, this translation was implemented almost like a macro.  But in any case, queries that are implemented via method syntax rely, of course, on the extension methods that are included with the .NET framework, and hence query expressions also rely on them.  It is the ability to write extension methods for generic interfaces that enables queries.

[Table of Contents] [Next Topic] [Blog Map]