Why should we explicitly specify "this" when calling the extension method from the extended class method?

public class StoreController : Controller { public string Index() { // implicitly specified instance does not work //return GetMemberName(); // must specify this explicitly return this.GetMemberName(); } } public static class Utilities { public static string GetMemberName(this Controller caller, [CallerMemberName] string memberName = "") { return caller.GetType().FullName + "." + memberName; } } 

Why should we explicitly specify this when calling the extension method from the extended class method?

In my mental model, we can usually omit this , for example, when we initialize the fields, for example.

+4
source share
3 answers

Without attending a meeting of the project committee where this was decided, it is difficult to say why this is so.

In this text, I use a method or instance in the sense of a function associated with a specific instance of an object, and I use a function in a mathematical sense. The function takes several arguments and produces a result (which is potentially invalid)

If we do not consider virtual methods, which are more complex, because the actual function that will be called is determined by the execution time, then any method calls are syntactic sugar. If we have two methods described below

 internal static class Extensions { public static string FooEx(this MyClass self){ return self.ToString(); } } internal class MyClass { public string Bar(){ var s1 = Foo(); var s2 = this.FooEx(); } private string Foo(){ return ToString(); } } 

Both will then be translated into a function call, where the first (and only) argument in both cases will be the object identified by this . If in doubt, look at the IL created for any call to the instance method, and you will notice an additional argument there compared to the declaration in the code. This argument is the this reference, which is always passed as the first argument to the instance method.

Therefore, in the case of the instance method, the compiler still needs to determine which object to pass as the first argument to the function. This is exactly the same thing that it should do if you call the extension method without this , which also means that this cannot be the real reason why you need to use this before the extension method.

In the compiler for Marvin , the compiler compiler on top of the Mono compiler, I had to do a similar trick, since C # does with extension methods and wondered why the specifications require this real reason that the compiler forces you to use this before the extension method says what the specs say about this. What is the reason for this decision, it will take the attention of someone like @EricLippert, which is probably there when they solved this requirement

+1
source

The extension method is not technically a method associated with your class.

yourClass.ExtensionMethod() does not match yourClass.ClassMethod() .

Basically what you do is a convenient way to do this:

 ExtensionMethod(YourClass yourClass) { //do something return yourClass; } 

This is my understanding of the extension method. This is a convenient way to call a method against a class that you cannot change. So why can't you just call it without this. This is not a class method.

+3
source

An extension method is just syntactic sugar for invoking a static method. This line of code return this.GetMemberName(); actually converted to a call to a static method such as Utilities.GetMemberName(this);

As you can see, you need to send this to the static method, and for this very reason you need this keyword.

+2
source

Source: https://habr.com/ru/post/1440221/


All Articles