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?

 

NB! This blog is moved to gunnarpeipman.com

Click here to go to article

6 Comments

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

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

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

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

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

  • No problem. Thank you for comments! :)

Comments have been disabled for this content.