static void Main(string[] args)
{
int test = 1;
resetTest();
Console.Write(test); // Should be 0
}
static void resetTest()
{
test = 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么不起作用?我怎么才能得到它?(我不想使用int函数并返回变量)现在我有一条错误消息,说明函数resetTest上的变量test是未定义的.
因为test是在Main方法的范围内定义的.
你需要将它移到该方法之外才能使其工作.
就像是:
static int test = 1;
static void Main(string[] args)
{
resetTest();
Console.Write(test); // Should be 0
}
static void resetTest()
{
test = 0;
}
Run Code Online (Sandbox Code Playgroud)