Wednesday, October 13, 2010

Extension Methods

" Software Engineers never die… They just go Offline ….  "

Extension Methods
Extension methods let you add custom methods to previously defined types, even if those types are
sealed in C# or NotInheritable in VB. For example, the string / String class is sealed and has 45
predefined instance methods, which you access by dot ( . ) syntax, as in strTest.Length() or “ This
is a test ” .Length() . The compiler treats these statements as if they were members of a function
library: Length(strTest) or Length( “ This is a test ” ) .
The Length method throws an exception if the class value is null or Nothing , so a LengthNullable()
method that returns an int? , Integer? , or Nullable(Of Integer) type eliminates the need to check
for null or Nothing before invoking the Length method. Here ’ s the C# 3.0 code for the
LengthNullable method:
C# 3.0
static class ExtensionMethods
{
public static Int32? LengthNullable(this string test)
{
if (test != null)
{
return test.Length;
}
else
{
return null;
}
}
}
Sample code for the C# 3.0 ExtensionMethods class is located in the ExtensionMethods.cs
class file.

According to VS 2008 ’ s online help ’ s “ extension methods [Visual Basic] ” topic, you can apply extension
methods to any of the following types:
Classes (reference types)
Structures (value types)
Interfaces
Delegates
ByRef and ByVal arguments
Generic method parameters
Arrays
C# 3.0 extension methods must be defined by a public static method within a public static or
internal static class. Prefixing the method ’ s first argument with the this keyword (highlighted in
the code) informs the compiler that the function is an extension method. The second argument is the
type to which the method applies, string for this example, and the third is the instance name, test .
Extension methods support full statement completion and IntelliSense features.
A downward - pointing arrow added to the IntelliSense list ’ s method icon identifies extension methods.
Following are a few C# test methods to verify that the new extension method behaves as expected:
C# 3.0
// Import the namespace if method is in another project
using CS3Extensions.ExtensionMethods;
// Extension method tests
string nada = null;
string test = “This is a test”;
int? len0 = nada.LengthNullable();
int? len1 = test.LengthNullable();
int? len2 = “This is a test”.LengthNullable();
You can create VB 9.0 extension methods in modules only, and you must add an Imports System
.Runtime.CompilerServices directive to support the < Extension() > attribute. This attribute is
necessary to instruct the compiler that the function is an extension method. VB functions declared within
modules are static, so the Shared keyword isn ’ t required. The argument ’ s type declaration specifies the
class to which the extension method applies.
VB 9.0
Imports System.Runtime.CompilerServices
Public Module ExtensionMethods
‘ VB extension methods can only be declared within modules
< Extension() > _
Public Function LengthNullable(ByVal Test As String) As Integer?
If Test Is Nothing Then
Return Nothing
Else
Return Test.Length
End If
End Function
End Module
Sample code for the VB 9.0 ExtensionMethods module is located in the ExtensionMethods.vb
module file.
As with the C# version, you must import the VB module ’ s namespace to bring the extension method
within the application ’ s scope, if the module isn ’ t in the same project. The VB test code is quite similar to that for the C# version.

No comments:

Post a Comment