9

A few questions about Extension Methods:

  1. Why are Extension Methods static?

  2. Why do they need to be declared in a static class?

  3. What does the this keyword indicate in the parameter list of extension method? As it is a static class how does the "this" keyword work in this context?

  4. How does memory allocation happen for these type of methods?

user247702
  • 21,902
  • 13
  • 103
  • 146
Nishant
  • 885
  • 16
  • 33
  • 4
    http://msdn.microsoft.com/en-us/library/bb383977.aspx – Gabriel Apr 20 '11 at 12:13
  • i'm especially interested in the memory allocation aspect of extention methods, since linq uses these everywhere. – Alexandre Brisebois Apr 20 '11 at 12:17
  • The show is run by **CompilerServices.ExtensionAttribute** – V4Vendetta Apr 20 '11 at 12:18
  • 1
    [Why do they need to be declared in a static class?](http://stackoverflow.com/questions/3930335/why-are-extension-methods-only-allowed-in-non-nested-non-generic-static-class) [What does the this keyword indicate?](http://stackoverflow.com/questions/2574311/why-we-use-this-in-extension-methods) – Matt Ellen Apr 20 '11 at 12:19
  • 2
    @Alexandre, @nasmifive What kind of memory allocation aspect are you expecting an extension method to have? It's just a regular method... – Ilya Kogan Apr 20 '11 at 12:24

5 Answers5

8

The only difference between a static and a non-static method is that a non-static method receives an implicit parameter - this. Extension methods are not called in the context of the object on which the method is declared, therefore there is no way to pass them the this reference, therefore they must be static.

You cannot use the keyword this inside an extension method, I hope this answers your third question. The this keyword in the parameter list is there just to indicate which type this method extends.

What is your question about memory allocation? An extension method is just like any other static method, only the call syntax is different.

Ilya Kogan
  • 20,368
  • 15
  • 78
  • 134
  • Hmm, don't ordinary non-static methods also receive an implicit `this` parameter? Or is that an implementation detail that's specific to C++? – Cody Gray Apr 21 '11 at 07:27
  • @Cody Yes, they do. For some reason @Robert Harvey edited my answer to something wrong and different from what I meant. I rolled it back now. – Ilya Kogan Apr 21 '11 at 07:46
4

Why are Extension Methods static?

Because you cannot modify the class to add instance methods. It's all syntactic sugar to make calls to these methods nicer, nothing more.

Why do they need to be declared in a static class?

Because we're not meant to create instances of the class. It's a design decision. Why complicate things like having instances of some class that has extension methods attached to them that might not even be usable?

What does the this keyword indicate in the parameter list of extension method? As it is a static class how does the "this" keyword work in this context?

It's a static method. this has no meaning in this context. The this in the signature is just a way to denote that an extension method is being declared.

How does memory allocation happen for these type of methods?

Confused about this one. They are all static methods on a static class. They are "allocated" like any other (static) method, there's only one copy of the code for the type.

Jeff Mercado
  • 113,921
  • 25
  • 227
  • 248
1
  1. Because there is no instance to call them on.

  2. I think its to limit number of possible errors if it was declared in non-static class.

  3. It says its extension method. Compiler sees this and allows to use it on type declared as this, instead as static method.

  4. Extension method call is only syntactic sugar. This call gets converted into ordinary static method call in IL.

Euphoric
  • 12,015
  • 1
  • 26
  • 41
1

The answer to 1,2, and 4 is pretty much the same. Extension methods are simply static methods with some special syntactical sugar. This:

public static void DoIt(this string str) {
    // ..
}

"test".DoIt();

Is effectively the same IL as:

public static void DoIt(string str) {
    // ..
}

DoIt("test");

The former method makes it much easier to provide IntelliSense support though. The restriction of the class needing to be static is probably just a design decision, or maybe performance related.

The answer to #3 is that's just the syntax the language designers chose. C# developers are used to this refering to the instance object. They could have called it blah, but it wouldn't have been as obvious as to what the instance object is.

Using the this keyword in an extension method also tells the C# compiler to mark it with the ExtensionAttribute. This attribute is used by consumers of the assembly to find extension methods.

CodeNaked
  • 38,487
  • 6
  • 108
  • 141
0

That's just how they're defined - with this keyword preceding the first parameter, which is the instance of the object you're calling the method on. Why is ToString() called ToString()? Why is static used for static? Why is the grass green? I guess the powers that be decided to put a this in front of the first parameter, instead of coming up with a new keyword "extension" to put in front of method name.

IL-generated by the compiler translates your code into a call on the static method, even though the syntax may look like a call on an instance method. But it's not - it's a static, which has no access to your object's private members.

Kon
  • 25,664
  • 11
  • 56
  • 84