NDeveloper
NDeveloper

Reputation: 1847

How extension methods are implemented internally

How are extension methods implemented internally? I mean what happens when the compiler sees a declaration for an extension method and what happens at runtime when there is a call to an extension method.

Is reflection involved? Or when you have an extension method is its code injected in the target class type metadata with some additional flags noting that this is an extension method and then the CLR knows how to handle that?

So in general, what happens under the hood?

Upvotes: 9

Views: 1801

Answers (7)

Incognito
Incognito

Reputation: 16597

As already have said by other colleagues it is just a static method. It is all about the compiler we can say that CLR even have no idea about extension methods. You can try to check IL code ..

Here is an example

static class ExtendedString
{
    public static String TestMethod(this String str, String someParam)
    {
        return someParam;
    }
}

static void Main(string[] args)
{
    String str = String.Empty;
    Console.WriteLine(str.TestMethod("Hello World!!"));
    ........
}

And here is the IL code.

  IL_0001:  ldsfld     string [mscorlib]System.String::Empty
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  ldstr      "Hello World!!"
  IL_000d:  call       string StringPooling.ExtendedString::TestMethod(string,
                                                                       string)
  IL_0012:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0017:  nop

As you can see it is just a call of static method. The method is not added to the class, but compiler makes it look like that. And on reflection layer the only difference you can see is that CompilerServices.ExtensionAttribute is added.

Upvotes: 11

Einar
Einar

Reputation: 312

Yes, extension methods are just static methods. They have no extra privileges with respect to the classes they supposedly "extend". However, the compiler does mark the "extension" static method with an ExtensionAttribute. You can see this in the IL. This makes the compiler treat it specially, so that you cannot actually invoke it as a regular static method. For instance, this won't compile:

var test = new [] { "Goodbye", "Cruel", "World" };
var result = IEnumerable<string>.Where<string>(test, s => s.Length > 5);

Even though this is what happens under the hood.

But as LukeH notes below, you can invoke it on the class where it's actually defined... Silly me.

Upvotes: 0

V4Vendetta
V4Vendetta

Reputation: 38230

Extension methods are very much like static methods with the only difference in it having an attribute CompilerServices.ExtensionAttribute which helps in identifying it as an Extension method.

You can read this

Upvotes: 0

Simon Mourier
Simon Mourier

Reputation: 139256

Extension methods are just static methods. The only difference is brought by tools such as the Visual Studio editor which pop them up for the auto complete (intellisense) feature. You can find a detailed explanation here: C# Extension Methods: Syntactic Sugar or Useful Tool?

Upvotes: 0

Glenn Ferrie
Glenn Ferrie

Reputation: 10408

To clarify the above answer... Extension Methods ARE static functions. The additional features in .NET 3.5 allow them to be interpreted as if they are new methods on the type in question.

Upvotes: 1

FIre Panda
FIre Panda

Reputation: 6637

I dont think that reflection is involved in extension methods. The extension method is handled in the same way like you write a static helper function in a helper class, the only difference is that compiler does it for you.

Upvotes: 2

extension methods are converted to static functions.In other words,They are syntactic sugar for static functions.

Upvotes: 4

Related Questions