C# 中关于类初始化时内部变量的执行顺序

class A
    {
        private static int i = 10;
        static A()
        {
            Console.WriteLine("Init A");
        }
        public A()
        {
            i = 100;
            Console.WriteLine(i.ToString());
        }
        public static void F()
        {
            Console.WriteLine("A.F");
        }
        public  void F1()
        {
            Console.WriteLine("A.F1");

        }

}

 当这个类被A a=new A();的时候,首先执行的是 private static int i = 10;这个静态变量,接着执行  static A()
        {
            Console.WriteLine("Init A");
        }静态构造函数,其次执行构造函数 public A()
        {
            i = 100;
            Console.WriteLine(i.ToString());
        }

 

posted @ 2010-05-04 16:38  HIT上帝视角  阅读(402)  评论(0编辑  收藏  举报