On the subject of the initialization sequence

When constructing a c # object understood that the order of object fields and constructors are important:
  1. Derived static fields // derived class static fields
  2. Derived static constructor // constructor derived statically
  3. Derived instance fields // derived class instance fields
  4. Base static fields // base class static fields
  5. Base static constructor // static base class constructor
  6. Base instance fields // base class instance field
  7. Base instance constructor // base class instance constructors
  8. Derived instance constructor // derived class instance constructors
The following is a simple code:
 1 using  System;
 2
 3 namespace  ObjectInit
 4 {
 5    class Program
 6    {
 7        static void Main( string[] args )
 8        {
 9            Derived d = new Derived();
10            Console.ReadLine();
11        }

12    }

13    class Base
14    {
15        public Base()
16        {
17            Console.WriteLine( "Base.Instance.Constructor" );
18            this.m_Field3 = new Tracker( "Base.Instance.Field3″ );
19            this.Virtual();
20        }

21        static Base()
22        {
23            Console.WriteLine( "Base.Static.Constructor" );
24        }

25        private Tracker m_Field1 = new Tracker( "Base.Instance.Field1″ );
26        private Tracker m_Field2 = new Tracker( "Base.Instance.Field2″ );
27        private Tracker m_Field3;
28        static private Tracker s_Field1 = new Tracker( "Base.Static.Field1″ );
29        static private Tracker s_Field2 = new Tracker( "Base.Static.Field2″ );
30        virtual public void Virtual()
31        {
32            Console.WriteLine( "Base.Instance.Virtual" );
33        }

34    }

35    class Derived : Base
36    {
37        public Derived()
38        {
39            Console.WriteLine( "Derived.Instance.Constructor" );
40            this.m_Field3 = new Tracker( "Derived.Instance.Field3″ );
41        }

42        static Derived()
43        {
44            Console.WriteLine( "Derived.Static.Constructor" );
45        }

46        private Tracker m_Field1 = new Tracker( "Derived.Instance.Field1″ );
47        private Tracker m_Field2 = new Tracker( "Derived.Instance.Field2″ );
48        private Tracker m_Field3;
49        static private Tracker s_Field1 = new Tracker( "Derived.Static.Field1″ );
50        static private Tracker s_Field2 = new Tracker( "Derived.Static.Field2″ );
51        override public void Virtual()
52        {
53            Console.WriteLine (  " Derived.Instance.Virtual "  );
54 is         }
55     }
56 isclass Tracker57 is { 58 public  Tracker (  String  text) 59 { 60             Console.WriteLine (text); 61 is         } 62 is     } 63 is}
    
    
        
        



6465The following is a procedure Results of the:


Derived.Static.Field1
Derived.Static.Field2
Derived.Static.Constructor
Derived.Instance.Field1
Derived.Instance.Field2
Base.Static.Field1
Base.Static.Field2
Base.Static.Constructor
Base.Instance.Field1
Base.Instance.Field2
Base.Instance.Constructor
Base.Instance.Field3
Derived.Instance.Virtual
Derived.Instance.Constructor
Derived.Instance.Field3

C#对象初始化的原则 :


  • Fields then Constructor.  //先初始化字段,后执行构造函数.
  • Static then Instance. //在第一次访问类时,先初始化静态字段和构造函数,后初始化对象的实例字段和构造函数.
  • Derived then Base. // 不论字段还是静态构造函数,派生类对象总是先于基类对象初始化.
  • Except Instance Constructors.  //对于实例构造函数,基类构造函数总是先于派生类的实例构造函数初始化.
  • Don't Assume Field Order.  //字段的初始化顺序总是按照他们在源文件的定义顺序执行的.
  • Two-Phase for Virtual. //避免从构造函数中调用虚拟方法.

转载于:https://www.cnblogs.com/yangjie5188/archive/2008/02/22/1077531.html

Guess you like

Origin blog.csdn.net/weixin_34008805/article/details/93499931