Why keyword 'this' cannot be used in a sta

2020-02-10 03:30发布

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?

[Update]: Actually, this is a question I got in an interview. I do know the usage of 'static' and 'this', based on all your response, I guess I know a little of why the two can not be used together. That is, for static method is used to changed state or do something in a type level, but when you need to use 'this' means you want to change the state or do something in a instance level. In order to differentiate the state change of a type and the state change of an instance, then c# donot allow use 'this' in a static method. Am I right?

13条回答
做自己的国王
2楼-- · 2020-02-10 04:08

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.

查看更多
倾城 Initia
3楼-- · 2020-02-10 04:11

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);
    }
}
查看更多
smile是对你的礼貌
4楼-- · 2020-02-10 04:12

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.

查看更多
Melony?
5楼-- · 2020-02-10 04:13

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.

查看更多
做个烂人
6楼-- · 2020-02-10 04:14

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.

查看更多
冷血范
7楼-- · 2020-02-10 04:14

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.

查看更多
登录 后发表回答