public static void DoSomething()
{
int a;
string b;
//..do something
}
在上面的例子中,我声明了两个变量。 它们变成静态的是因为包含它们的方法是静态的吗?
最佳答案
没有。只有方法是静态的而不是变量。
来自 MSDN:
C# does not support static local variables (variables that are declared in method scope).
如果想在静态成员中有静态变量,在静态方法外声明,
private static int _var = 0;
public static void SampleMethod()
{
_var++;
}
关于c# - 静态方法中的变量是否会自动变为静态,因为它们在 c# 中的静态范围内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224050/