为什么不能在.Net中的静态方法中使用关键字“this”?

时间:2023-01-07 22:45:24

I'm trying to use the this keyword in a static method, but the compiler won't allow me to use it.

我试图在静态方法中使用this关键字,但编译器不允许我使用它。

Why not?

为什么不?

7 个解决方案

#1


63  

That's an easy one. The keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class. There is a much more in depth explanation of what static members are and why/when to use them in the MSDN docs.

这很简单。关键字“this”返回对包含它的类的当前实例的引用。静态方法(或任何静态成员)不属于特定实例。它们存在而不创建类的实例。有关静态成员以及在MSDN文档中使用它们的原因/时间的更深入解释。

#2


8  

As an additional note, from a Static method, you can access or static members of that class. Making the example below valid and at times quite useful.

另外请注意,从Static方法中,您可以访问该类的静态成员。使下面的示例有效,有时非常有用。

public static void StaticMethod(Object o)
{
     MyClass.StaticProperty = o;
}

#3


5  

Static methods are Class specific and not instance specific. "this" represents an instance of the class at runtime, so this can't be used in a static context because it won't be referencing any instance. Instead the class's name should be used and you would only be able to access static members in the class

静态方法是特定于类的,不是特定于实例的。 “this”表示运行时类的实例,因此不能在静态上下文中使用它,因为它不会引用任何实例。相反,应该使用类的名称,并且您只能访问类中的静态成员

#4


1  

this represents the current instance object and there is no instance with static methods.

这表示当前实例对象,并且没有静态方法的实例。

#5


1  

There is no this object reference in the static method.

静态方法中没有此对象引用。

#6


1  

If You want to use non static function of class in static function.Create object of class in static function. For Eg

如果要在静态函数中使用类的非静态函数。在静态函数中创建类的对象。对于Eg

    Class ClsProgram(){
public static void staticfunc(){
ClsProgram Obj = new ClsPrograM()
Obj.NonStaticFunc();
}
public void NonStaticFunc(){}
}

#7


0  

For OP's question, refer to the accepted answer. This answer is for the ones who're looking for a fast one liner to use in static methods.

对于OP的问题,请参阅接受的答案。这个答案适用于那些正在寻找快速单线程用于静态方法的人。

If the class is a form, and it's open (you need the name of the form as well), this can be called within a static method;

如果类是一个表单,并且它是打开的(您还需要表单的名称),则可以在静态方法中调用它;

Application.OpenForms["MainForm"];

#1


63  

That's an easy one. The keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class. There is a much more in depth explanation of what static members are and why/when to use them in the MSDN docs.

这很简单。关键字“this”返回对包含它的类的当前实例的引用。静态方法(或任何静态成员)不属于特定实例。它们存在而不创建类的实例。有关静态成员以及在MSDN文档中使用它们的原因/时间的更深入解释。

#2


8  

As an additional note, from a Static method, you can access or static members of that class. Making the example below valid and at times quite useful.

另外请注意,从Static方法中,您可以访问该类的静态成员。使下面的示例有效,有时非常有用。

public static void StaticMethod(Object o)
{
     MyClass.StaticProperty = o;
}

#3


5  

Static methods are Class specific and not instance specific. "this" represents an instance of the class at runtime, so this can't be used in a static context because it won't be referencing any instance. Instead the class's name should be used and you would only be able to access static members in the class

静态方法是特定于类的,不是特定于实例的。 “this”表示运行时类的实例,因此不能在静态上下文中使用它,因为它不会引用任何实例。相反,应该使用类的名称,并且您只能访问类中的静态成员

#4


1  

this represents the current instance object and there is no instance with static methods.

这表示当前实例对象,并且没有静态方法的实例。

#5


1  

There is no this object reference in the static method.

静态方法中没有此对象引用。

#6


1  

If You want to use non static function of class in static function.Create object of class in static function. For Eg

如果要在静态函数中使用类的非静态函数。在静态函数中创建类的对象。对于Eg

    Class ClsProgram(){
public static void staticfunc(){
ClsProgram Obj = new ClsPrograM()
Obj.NonStaticFunc();
}
public void NonStaticFunc(){}
}

#7


0  

For OP's question, refer to the accepted answer. This answer is for the ones who're looking for a fast one liner to use in static methods.

对于OP的问题,请参阅接受的答案。这个答案适用于那些正在寻找快速单线程用于静态方法的人。

If the class is a form, and it's open (you need the name of the form as well), this can be called within a static method;

如果类是一个表单,并且它是打开的(您还需要表单的名称),则可以在静态方法中调用它;

Application.OpenForms["MainForm"];