提问者:小点点

如何在类之间传递变量


我得到了这个错误:

“变量'a'已赋值,但其值永远不可用”

namespace test
{
    public class Program
    {
        static void Main(string[] args)
        {
            test();
            Console.WriteLine(a);
            Console.ReadLine();
        }

        public static void test()
        {
            int a = 11;
        }
    }
}

共1个答案

匿名用户

你说的是“方法”而不是类。您可以在此阅读更多信息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;
        }
    }
}

相关问题