我得到了这个错误:
“变量'a'已赋值,但其值永远不可用”
namespace test
{
public class Program
{
static void Main(string[] args)
{
test();
Console.WriteLine(a);
Console.ReadLine();
}
public static void test()
{
int a = 11;
}
}
}
你说的是“方法”而不是类。您可以在此阅读更多信息https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/与结构/方法
但同时,试试这个:
namespace test
{
public class Program
{
static void Main(string[] args)
{
int a = test();
Console.WriteLine(a);
Console.ReadLine();
}
public static int test()
{
int a = 11;
return a;
}
}
}