Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How extension methods hook up

Tags:

c#

.net

I was just curious to know how Extension methods are hooked up to the Original class. I know in IL code it gives a call to Static Method, but how it does that and why dosen't it break encapsulation.

like image 946
Shekhar_Pro Avatar asked Dec 18 '10 16:12

Shekhar_Pro


People also ask

How do extension methods work?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What are extension methods explain with an example?

An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example.

Where do you put extension methods?

An Extension Method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement. You can give any name of for the class that has an Extension Method but the class should be static.


3 Answers

They don't "hook up".

The Visaul Studio IDE just makes it look like it does by showing them in the intellisense lists.

The compiler "knows" how to deal with the references in order to make the right method calls with the correct parameters.

This is simply syntactic sugar - the methods are simply static methods on a separate static class. Using the this modifier lets the compiler "know" to add the ExtensionAttribute to the class to mark it as an extension method.

Since extension methods do not in fact change the class and can only access public members on it, encapsulation is retained.

From MSDN:

Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

(emphasis mine)

like image 193
Oded Avatar answered Oct 12 '22 23:10

Oded


Extension methods are specified by putting the this keyword in front of the first parameter of a static method:

public static void SomeExtension(this string s)
{
    ...
}

That is just syntactic sugar for decorating the method with System.Runtime.CompilerServices.ExtensionAttribute:

[Extension]
public static void SomeExtension(string s)
{
    ...
}

When the compiler sees that attribute, it knows to translate the extension method call to the appropriate static method call, passing the instance as the first parameter.

Since the calls are just normal static method calls, there is no chance to break encapsulation; the methods, like all static methods, only have access to the public interfaces of the extended types.

like image 36
Bryan Watts Avatar answered Oct 12 '22 23:10

Bryan Watts


Extension methods are just syntactic sugar, they are just static methods. You are only able to access public fields or properties in them, just like normal static methods.

like image 21
Femaref Avatar answered Oct 13 '22 00:10

Femaref