代码如下,我在test类里定义了一个Program类的字段,实例化test类的一个实例后为什么不能不能访问Program的字段i.
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication3
{
    class Program
    {
       public int i;
        public Program()
        {
            i = 10;        }
    }
    class test
    {
        Program p;
        static void Main()
        {
            test t = new test();
            Console.WriteLine(t.p.i);
        }
    }
}

解决方案 »

  1.   

    应该要new出一个实例:
    Program p=new Program();
      

  2.   

    test类中的Program p,p没有实例化,因而无法使用p
      

  3.   

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                test t = new test();
                Console.WriteLine(t.i);
            }
        }    class test
        {
            public int i;
            public test()
            {
                i = 10;
            }
        }}