A few questions about Extension Methods:
Why are Extension Methods static?
Why do they need to be declared in a static class?
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With