Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

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;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

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.

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.

Comments

mehfuzh said:

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.

# December 29, 2007 12:42 PM

DigiMortal said:

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.

# December 29, 2007 3:16 PM

Trumpi's blog said:

My router&#39;s WAN port died today. Any recommendations for a good gigabit/s router? Podcasts Hanselminutes

# December 29, 2007 4:50 PM

some guy said:

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".

# January 2, 2008 2:14 PM

DigiMortal said:

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

# January 2, 2008 2:52 PM

some guy said:

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.

# January 2, 2008 3:11 PM

DigiMortal said:

No problem. Thank you for comments! :)

# January 2, 2008 3:15 PM

Gunnar Peipman's ASP.NET blog said:

Getting distinct values from arrays is not a unique problem. Here will I show some options how to do

# May 15, 2008 4:02 AM

Gunnar Peipman's ASP.NET blog said:

.Net 2.0 provided us with new feature called partial classes . Using partial classes we can use multiple

# June 7, 2008 6:30 PM

Getting distinct values from arrays (through .NET Framework history) | Gunnar Peipman - Programming Blog said:

Pingback from  Getting distinct values from arrays (through .NET Framework history)   | Gunnar Peipman -  Programming Blog

# May 7, 2013 3:32 AM

C# and Partial Classes | Gunnar Peipman - Programming Blog said:

Pingback from  C# and Partial Classes   | Gunnar Peipman -  Programming Blog

# May 15, 2013 12:50 PM