Extension methods – how they look like after compiling

This blog entry is supposed to be the continuation for entry C# Extension Methods where I told what extension methods are and how to use them. This time I will tell about what they are behind the curtains. Do they really add new methods to current classes or not? I mean, are these methods really added to classes or are they something else?

Let’s start with extension that adds Nl2Br() method to string. This function is well-known from PHP programming language. So, here’s the code.


using System;

namespace
csharptest
{
   
public static class Ext
    {
       
public static string Nl2Br(this string
s)
        {
           
return s.Replace("\r\n", "<br />"
)
                    .Replace(
"\n", "<br />"
);
        }
    }

   
class Program
    {
       
static void Main(string
[] args)
        {
           
string s = "blah\r\nblah"
;
           
Console
.WriteLine(s.Nl2Br());

           
Console.ReadLine();
        }
    }
}

Now when we look the methods of string we will see Nl2Br() method also there.

Extension methods are marked with down arrow in IntelliSense

That’s right, we see this method there but we cannot be sure if it is really there. So, is this new method illusion or real? Let’s compile our code and see what it looks like in intermediate language.


.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
 
// Code size       26 (0x1a)
  .maxstack  1
  .locals init ([0]
string
s)
  IL_0000:  nop
  IL_0001:  ldstr     
"plah\r\nplah"
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  call      
string csharptest.Ext::Nl2Br(string
)
  IL_000d:  call      
void [mscorlib]System.Console::WriteLine(string
)
  IL_0012:  nop
  IL_0013:  call      
string
[mscorlib]System.Console::ReadLine()
  IL_0018:  pop
  IL_0019:  ret
}
// end of method Program::Main

Look at the line where Nl2Br() is called. As we can see Nl2Br() method is not part of string class. Compilator translates all calls to extension methods to calls to static class methods. So, telling that compiler creates new methods to classes is completely wrong as we just saw.


8 thoughts on “Extension methods – how they look like after compiling

  • mehfuzh says:

    I read both, of your posts, really nice!!, here i can see that , when a method is extended , using "this" before a type, it comes to method list of that type and when called , the complier forwards the call to original method.

    >>So, telling that compiler creates new methods to classes is completely wrong as we just saw.

    Do you, mean for non-static types the complier creates a new class ?

    Thanks,

    Mehfuz.

  • Gunnar says:

    I mean that extension methods will be no part of class they are extending. Looking at my example, Nl2Br() will be no method of string class.

  • some guy says:

    i think you misunderstood what is going on. extension methods are microsoft’s name for monkey patching. the definition of a monkey patch being "a way to extend or modify runtime code without altering the original source code".

  • Gunnar says:

    Thanks for comment, some guy. Unfortunately it is not me who misunderstoods extension methods. I understand them well. But there are many other programmers who have no idea what it is and what it is. So I wrote this blog entry to illustrate how extension methods work. 🙂

  • some guy says:

    sorry if i came across as critical, my written english is not the best. i thought you had a clear explanation of what was going on at the intermediate language level but just wanted to add the definition of what is going on as it reinforces your explanation.

  • Gunnar says:

    No problem. Thank you for comments! 🙂

  • […] powerful things we can use to be more productive, faster and better. We have automatic properties, extension methods, partial classes, generics and so on. If we don’t know what they are and how these things are […]

  • […] methods are pretty powerful thing because we can extend existing types without inheriting from them. I don’t know developers who want to extend string or int or other basic types offered by […]

Leave a Reply

Your email address will not be published. Required fields are marked *