开发者

Why are "static" and "this" needed for extension methods, and how is their memory allocated?

A few questions abo开发者_如何转开发ut 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?


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.


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.


  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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜