为什么关键字“this”?不能在静态方法中使用?
为什么静态方法中不能使用关键字this
?我想知道为什么 C# 定义了这个约束。通过这种约束可以获得什么好处?
[更新]: 其实这是我在采访中被问到的一个问题。 我确实知道“static”和“this”的用法,根据您的所有回复,我想我知道为什么两者不能一起使用。也就是说,静态方法用于更改状态或在类型级别执行某些操作,但是当您需要使用“this”时意味着您想要更改状态或在实例级别执行某些操作。为了区分类型的状态变化和实例的状态变化,c# 不允许在静态方法中使用“this”。我说得对吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
因为
this
指向类的实例,所以在静态方法中没有实例。您会注意到静态成员的定义是
这就是为什么
this
没有任何指向的原因。Because
this
points to an instance of the class, in the static method you don't have an instance.You'll notice the definition of a static member is
Which is why
this
has nothing to point to.this
是当前对象的实例。对于静态方法,没有当前对象,因此this
不存在。它并不是真正的约束,而是方法的全部要点是静态的。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.此
引用到类的当前实例,因此只能在实例方法中使用。静态方法作用于类级别,没有实例。因此,没有this
。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, nothis
.this
指的是对象的当前实例。静态方法是类上的方法。它不是实例方法,因此在静态方法中使用它是没有意义的。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.我很确定这不仅限于 C#,也不是限制,而是合乎逻辑的情况。正如 @Yuriy 正确指出的那样, this 指的是类的当前实例,即您已经使用 new (或 DI)来实例化该类(创建了一个实例),并且您需要某种内部方式来引用该实例,即 this目的。在不实例化类的情况下调用静态方法,实际上没有创建任何对象,因此您无法访问该类的属性。
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.
通过静态方法,您可以编写:
与任何对象实例无关(因此您不需要this关键字)。
因为 static_method() 可以工作并且不需要对象实例来完成其工作。 static_method() 不知道您有哪个对象实例,但它可以改变行为
所有对象实例:
在这种情况下,static_method() 会更改 MyClass 的所有对象实例中的 display() 方法的行为。
By static methods you can write:
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:
In this case, static_method() changes the behavior of display() method in all object instances of MyClass.
关键字
this
指的是对象的实例。在静态上下文中,没有特定的实例可供引用。The keyword
this
refers to the instance of the object. In the static context there is not specific instance to reference.this
关键字可以用在标记为static
的方法中。该语法用于定义 扩展C# 中的方法。此功能自 2007 年发布的 C# 3.0 起就已提供(Wikipedia)在正常用法中,
this
指的是实例,static
表示不存在实例(因此没有this
)。事实上,从概念上理解this
和static
是什么,就可以自然地得出它们不能一起使用的事实(除了扩展方法等特殊例外)。The
this
keyword can be used in a method marked asstatic
. 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 nothis
). The fact that you can't use them together (aside from special exceptions like extension methods) follows naturally from understanding whatthis
andstatic
are, conceptually.this
用于引用变量或方法的父对象。当您在方法上声明static
时,可以调用该方法,而无需实例化该类的对象。因此,不允许使用this
关键字,因为您的静态方法未与任何对象关联。this
is used to refer to the parent object of a variable or method. When you declarestatic
on a method the method can be called without needing to instantiate an object of the class. Therefore thethis
keyword is not allowed because your static method is not associated with any objects.“this”指的是类的实例。静态是在没有实例化的情况下初始化的,因此静态方法不能引用未创建的“实例”。
'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.
对您的简短回答是:这指的是静态范围内不存在的类的实例。
但是,在进一步了解任何面向对象编程语言之前,请先寻找一本好书/课程,并尝试理解基本的面向对象概念。
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.
我不确定这是否有助于您的问题,但我相信这两个代码片段是等效的:
并且都
将调用 MyStaticClass 类中的 foo() 方法,假设您从 MyStaticClass 内部调用 foo()
编辑 - 最简单的方法记住静态类和非静态类之间的区别是想像java中的Math类。您可以调用 Math.abs(x);要获得 x 的绝对值,实例化 Math 对象并没有真正的意义,这就是 Math 是静态类的原因。
I am not sure if this helps your problem, but I believe these two code snippets are equivalent:
and simply
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.
另一个更字面的问题是:
“this”关键字不能在静态方法中使用,以避免与实例方法中的用法混淆,在实例方法中,它是访问自动传递的实例的指针(引用)的符号作为该方法的隐藏参数。
如果不是这样,您可以在静态方法中定义一个名为“this”的局部变量,但这与实例方法中引用的“this”关键字实例无关。
下面是一个具有两个等效方法的示例,一个是静态方法,另一个是实例方法。
这两个方法调用都会将单个参数传递给执行代码的方法,这些代码将执行相同的操作(在控制台上打印对象的字符串表示形式)并返回。
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.