Using the New Extension Methods Feature in C# 3.0

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.

Using the New Extension Methods Feature in C# 3.0

Extension Method Invocations

Table 1 shows how the method invocations are modified when the code is compiled.

Sr # Method Invocation Code Compiled As
1 expr . identifier ( ) identifier (expr)
2 expr . identifier ( args ) identifier (expr, args)
3 expr . identifier <typeargs> ( ) identifier <typeargs> (expr)
4 expr . identifier <typeargs> ( args ) identifier <typeargs> (expr, args)

Table 1. Method Invocation Modifications at Compile Time

If you check the IL of the main method through ILDASM, it will appear as follows:

.method private hidebysig static void  Main(string[] args) cil managed
{
   .entrypoint
   // Code size       42 (0x2a)
   .maxstack  1
   .locals init ([0] string s,
                 [1] int32 i,
                 [2] int32 j)
   IL_0000:  nop
   IL_0001:  ldstr      "9"
   IL_0006:  stloc.0
   IL_0007:  ldloc.0
   IL_0008:  call       int32 ExtensionMethods.EMClass::
                        ToInt32Ext(string)
   IL_000d:  stloc.1
   IL_000e:  ldloc.1
   IL_000f:  call       void [mscorlib]System.Console::WriteLine(int32)
   IL_0014:  nop
   IL_0015:  ldloc.0
   IL_0016:  call       int32 ExtensionMethods.EMClass::
                        ToInt32Static(string)
   IL_001b:  stloc.2
   IL_001c:  ldloc.2
   IL_001d:  call       void [mscorlib]System.Console::WriteLine(int32)
   IL_0022:  nop
   IL_0023:  call       string [mscorlib]System.Console::ReadLine()
   IL_0028:  pop
   IL_0029:  ret
}    // end of method Program::Main

The code marked in red indicates that the above method conversion (expr . identifier ( ) <--> identifier (expr) ) occurred.

So, when you call int i = s.ToInt32Ext();, the compiler internals convert it to int i = EMClass.ToInt32Ext(s);. Then, the rewritten form is processed as a static method invocation.

The identifier is resolved in the following order:

  1. The closest enclosing namespace declaration
  2. Each subsequent enclosing namespace declaration
  3. The containing compilation unit

The following is the order of precedence for methods in descending order:

  1. Instance methods
  2. Extension methods within the same namespace
  3. Extension methods outside the current namespace

Why Use Extension Methods?

You may be asking, "Why should I use extension methods when I have the regular static and instance methods?" Well, the answer simply is utter convenience. Let me explain with an example. Suppose you developed a library of functions over a period of years. Now, when someone wants to use that function library, the consumer must know the class name that defines the desired static method. Something like the following, for example:

a = MyLibraryClass.

At this point, IntelliSense will pop in and give you the names of the available functions. You just have to pick the one you need.

You then type your desired methods and pass the necessary argument:

a = MyLibraryClass.DesiredFunction(strName)

With this approach, you need to know beforehand which library contains your desired function and its name. With extension methods, it is more natural—something like the following:

a = strName.

At this point, IntelliSense pops up and shows which extension methods are available. You simply type the extension method you want:

a = strName.DesiredFunction()

No arguments are needed to identity the data type on which this method needs to work.

Invoke Static Methods on Object Instances

Extension methods provide a new mechanism for invoking static methods on object instances. But, as per C# 3.0 language specifications, extension methods are less discoverable and more limited in functionality than instance methods. Therefore, you should use extension methods sparingly, only where instance methods are not feasible.

Also, C# 3.0 is not yet an official release, so its specifications are not finalized. Therefore, the syntax is liable to change.

References

  1. http://msdn.microsoft.com/vcsharp/future/default.aspx
  2. Eric Lippert, Microsoft Developer, Visual C# Team.

About the Author

Vipul Patel is a Microsoft MVP (two years in a row) in Visual C# and currently works at Microsoft through Volt Information Sciences. He specializes in C# and deployment issues. You can reach him at Vipul_d_patel@hotmail.com.



About the Author

Vipul Vipul Patel

Vipul Patel is a Software Engineer currently working at Microsoft Corporation, working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Is your sales and operations planning helping or hurting your bottom line? Here are 5 useful tips from the experts at Quintiq to guide you to a better S&OP strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds