Extension Methods

Description Article
Table of Contents
Summary

Extension methods allows to create the illusion of new methods added to any existing type.

Description

Extension methods enable developers to add custom functionality to data types that are already defined without creating a new derived type (even to classes compiled into the .NET framework as a sealed class – System.String)

Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.

How it works

Extension methods are not members of the type they are extending. Technically the new methods are static methods defined on a custom class hided by the compiler.

In order to define an extension method:

  • The method cannot access protected and private members of the target obejct
  • The method cannot override or hide any instance method (according its signature)
  • The method can only be invoked after importing the new custom namespace where the extension methods are definied
Operations
Define an extension method on C#
Remarks

Extension methods on C# can only be defined on static classes.
The extension method has it’s first parameter as the target type being extended and marked with the keyword “this”

How to do it
public static class StringExtensions
{
    static public double ToDouble(this string pData)
    {
        double result = double.Parse(pData);
        return result;
    }

    static public int ToInt32(this string pData)
    {
        int result = Int32.Parse(pData);
        return result;
    }
}
How to use it
using StringExtensions;

string text = "43.35";
double data = text.ToDouble();
Define an extension method on VB.Net
Remarks

Extension methods on VB.Net can only be defined on public modules.
The extension method has it’s first parameter as the target type being extended and decorating the method with the extension attribute .

How to do it
Public Module StringExtensions

   <Extension()>
   Public Function ToDouble(string pData) As Double
        Dim result As Double = double.Parse(pData);
        return result;
   End Function

   <Extension()>
   Public Function ToInt32(this string pData) AS Int32
        Dim result As Int32 = Int32.Parse(pData);
        Return result;
   End Function

End Module
How to use it
Dim text As String = "43.35";
Dim data As Double = text.ToDouble();
Define an extension method on generic type on C#
How to do it
public static class GenericExtensions
{
    public static void DoSomething<T>(this MyGeneric<T> source)
    {
       // ...
    }

    public static IEnumerable<TResult> Where<TSource>(
                                this IEnumerable<TSource> source,
                                Func<TSource, TResult> predicate)
   {
       // ...
   }

}
Define an extension method on generic type on VB.Net
How to do it
Public Module GenericExtensions

   <Extension()>
   Public Sub DoSomething(Of T)(ByVal pData As MyGeneric(Of T))
      '...
   End Sub

   Public IEnumerable(Of TResult) DoSomething2(Of TSource)(
                                    IEnumerable(Of TSource) source,
                                    Func(Of In TSource, Out TResult)
       '...

    End Func

End Module