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

后端 未结 13 1518
遇见更好的自我
遇见更好的自我 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:28

    'this' refers to an instance of a class. Static is initialized without instantiation and hence the static method cannot refer to an 'instance' that is not created.

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

    I am not sure if this helps your problem, but I believe these two code snippets are equivalent:

    MyStaticClass.foo();
    

    and simply

    foo();
    

    will both call the foo() method in the MyStaticClass class, assuming you call foo() from inside MyStaticClass

    Edit - The easiest way to remember the difference between a static class and a non-static class is to think of something like the Math class in java. You can call Math.abs(x); to get the absolute value of x, and it does not really make sense to instantiate a Math object, which is why Math is a static class.

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

    By static methods you can write:

    MyClass.static_method();
    

    which there is nothing to do with any object instance (so you don't need this keyword).

    Because static_method() works and doesn't need object instances for its job. static_method() doesn't know which object instance do you have, but it can change the behavior of all object instances:

    MyClass a = new MyClass();
    MyClass b = new MyClass();
    MyClass.static_method("PRINTER");
    a.display(); //print something
    b.display(); //print something
    MyClass.static_method("MONITOR");
    a.display(); //display something on monitor
    b.display(); //display something on monitor
    

    In this case, static_method() changes the behavior of display() method in all object instances of MyClass.

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

    The short answer for you will be: this refers to an instance of a class which is non existing in a static scope.

    But please, look for a good book/class and try to understand basic object oriented concepts before going further on any object oriented programming language.

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

    this refers to the current instance of the object. A static method is a method on the class. It is not an instance method and therefore using this inside a static method is meaningless.

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

    I'm pretty sure this isn't limited to C# and it isn't a constraint, it's a logical situation. As @Yuriy correctly states, this refers to the current instance of a class, i.e. you've used new (or DI) to instantiate the class (created an instance of) and you need some way internally to refer to that instance, i.e. this object. A static method is called without instantiating the class, there is, in effect, no object created and as such you can't access properties of which this is one.

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