Using the New Extension Methods Feature in C# 3.0

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

One of the numerous exciting new features in C# 3.0 is extension methods: static methods that you can invoke using the instance method syntax. This article discusses this new feature in detail with sample code and recommendations for best use. (My previous article on C# 3.0 discussed anonymous types, another of the new features.)

Declaring Extension Methods

Extension method behavior is similar to that of static methods. You can declare them only in static classes. To declare an extension method, you specify the keyword this as the first parameter of the method, for example:

// Program.cs
public static class EMClass
{
   public static int ToInt32Ext(this string s)
   {
      return Int32.Parse(s);
   }

   public static int ToInt32Static(string s)
   {
      return Int32.Parse(s);
   }

}
class Program
{

   static void Main(string[] args)
   {
      string s = "9";
      int i = s.ToInt32Ext();              // LINE A
      Console.WriteLine(i);
      int j = EMClass.ToInt32Static(s);    // LINE B
      Console.WriteLine(j);
      Console.ReadLine();

   }
}

To compile the above code, you need to install Visual Studio 2005 and the LINQ preview. If you have Visual Studio 2005 installed already, you have three new project templates in the LINQ Preview under Visual C#: LINQ Console Application, LINQ Windows Application, and LINQ Library. Take the following steps to compile the code:

  1. Start the Visual Studio 2005 editor and create a new project, selecting LINQ Console as the project template in the New Project window.
  2. Name the project ExtensionMethods and click OK.
  3. Type the above code in the editor.
  4. Click F5 to compile the application and execute.

If you have only .NET 2.0 installed, you can do a command-line compile by using the following command:

Csc.exe /reference:"C:\Program Files\LINQ Preview\Bin
                      \System.Data.DLINQ.dll"
/reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll
/reference:"C:\Program Files\LINQ Preview\Bin\System.Query.dll"
/reference:"C:\Program Files\LINQ Preview\Bin\System.Xml.XLINQ.dll"
/target:exe Program.cs

As you can see from the above snippet, the differences between the extension method (ToInt32Ext) and the regular static method (ToInt32Static) are the following:

  1. Extension methods have the keyword this before the first argument. Static methods do not have the this keyword in its argument declaration.
  2. When extension methods are consumed, the argument that was declared with the keyword this is not passed. In the above code, Line A is an example of consuming the extension method ToInt32Ext. No argument is passed to it. When static methods are consumed, no arguments are skipped. All expected arguments must be entered. Line B is an example of this.
  3. Extension methods can be defined only in a static class. For static methods, this is not a requirement. Static methods can exist in a regular class as well as in a static class.
  4. Extension methods can be called only on instances values.

Extension methods, though static in nature, can be called only on instances. Trying to call them on a class will result in compilation errors. The class instances on which they are called are determined by the first argument in the declaration, the one having the keyword this.

Inside the IL

If you look at the IL generated from the executable compiled previously, you will see the display in Figure 1.

Figure 1. Extension Methods IL

The following is the IL for the extension method ToInt32Ext:

.method public hidebysig static int32 ToInt32Ext(string s) cil managed
{
   .custom instance void [System.Query]System.Runtime
   .CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
   // Code size       12 (0xc)
   .maxstack  1
   .locals init ([0] int32 CS$1$0000)
   IL_0000:  nop
   IL_0001:  ldarg.0
   IL_0002:  call       int32 [mscorlib]System.Int32::Parse(string)
   IL_0007:  stloc.0
   IL_0008:  br.s       IL_000a
   IL_000a:  ldloc.0
   IL_000b:  ret
}    // end of method EMClass::ToInt32Ext

The following is the IL for the static method ToInt32Static:

.method public hidebysig static int32 ToInt32Static(string s) cil managed
{
   // Code size       12 (0xc)
   .maxstack  1
   .locals init ([0] int32 CS$1$0000)
   IL_0000:  nop
   IL_0001:  ldarg.0
   IL_0002:  call       int32 [mscorlib]System.Int32::Parse(string)
   IL_0007:  stloc.0
   IL_0008:  br.s       IL_000a
   IL_000a:  ldloc.0
   IL_000b:  ret
}    // end of method EMClass::ToInt32Static

The code marked in red is present in the extension method:

.custom instance void: This line indicates that this method applies to instances only.

[System.Query]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ): This line indicates that the Extension attribute is flagged.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read