如何调用在C#中的静态方法的非静态方法? [英] How do I call a non-static method from a static method in C#?
本文介绍了如何调用在C#中的静态方法的非静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的code,我想打电话给 DATA1()
从数据2()
。这是可能在C#中?如果是这样,怎么样?
私人无效数据1()
{
}
私有静态无效数据2()
{
DATA1(); //生成错误
}
解决方案
您需要创建一个类的实例,并调用该方法就可以了。
公共类Foo
{
公共无效数据1()
{
} 公共静态无效数据2()
{
富富=新的Foo();
foo.Data1();
}
}
I have the following code, I want to call data1()
from data2()
. Is this possible in C#? If so, how?
private void data1()
{
}
private static void data2()
{
data1(); //generates error
}
解决方案
You'll need to create an instance of the class and invoke the method on it.
public class Foo
{
public void Data1()
{
}
public static void Data2()
{
Foo foo = new Foo();
foo.Data1();
}
}
这篇关于如何调用在C#中的静态方法的非静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文