Gunnar Peipman's ASP.NET blog

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






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

Click here to go to article


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 ?



# 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

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