May 23, 2019
Hot Topics:

Using the New Extension Methods Feature in C# 3.0

  • March 20, 2006
  • By Vipul Patel
  • Send Email »
  • More Articles »

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
   // 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::
   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::
   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.


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

Download the Code

You download the source code for this article here.

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.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date