Why keyword 'this' cannot be used in a static method?

后端 未结 13 1519
遇见更好的自我
遇见更好的自我 2020-12-16 14:13

Why can\'t the keyword this be used in a static method? I am wondering why C# defines this constraint. What benefits can be gained by this constraint?

[

相关标签:
13条回答
  • 2020-12-16 14:40

    The keyword this refers to the instance of the object. In the static context there is not specific instance to reference.

    0 讨论(0)
  • 2020-12-16 14:40

    Another, more literal, take on your question:

    The 'this' keyword can't be used in a static method to avoid confusion with its usage in instance methods where it is the symbol to access the pointer (reference) to the instance passed automatically as a hidden parameter to the method.

    If not by that you could possibly define a local variable called 'this' in your static method, but that would be unrelated to the 'this' keyword referenced instance in the instance methods.

    Below is an example with two equivalent methods, one static the other an instance method. Both method calls will pass a single parameter to the methods executing code that will do the same thing (print the object's string representation on the console) and return.

    public class Someclass {
    
      void SomeInstanceMethod() 
        { System.Console.WriteLine(this.ToString()); }
    
      void static SomeStaticMethod(Someclass _this) 
        { System.Console.WriteLine(_this.ToString()); }
    
      public void static Main()
        {
           Someclass instance = new Someclass();
           instance.SomeInstanceMethod();
           SomeStaticMethod(instance);
        }
    }
    
    0 讨论(0)
  • 2020-12-16 14:46

    this refers to the current instance of a class and can therefore be used only in instance methods. Static methods act on class level, where there are no instances. Hence, no this.

    0 讨论(0)
  • 2020-12-16 14:48

    Because this points to an instance of the class, in the static method you don't have an instance.

    The this keyword refers to the current instance of the class. Static member functions do not have a this pointer

    You'll notice the definition of a static member is

    Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object

    Which is why this has nothing to point to.

    0 讨论(0)
  • 2020-12-16 14:50

    this is an instance of the current object. With a static method, there is no current object, and as such, this doesn't exist. It's not really a constraint, but the entire point of a method being static.

    0 讨论(0)
  • 2020-12-16 14:50

    The this keyword can be used in a method marked as static. The syntax is used to define extension methods in C#. This feature has been available since C# 3.0, released in 2007 (Wikipedia)

    In the normal usage, this refers to the instance, static says that there is no instance (and therefore no this). The fact that you can't use them together (aside from special exceptions like extension methods) follows naturally from understanding what this and static are, conceptually.

    0 讨论(0)
提交回复
热议问题