浅谈对象的初始化顺序
当构造一个c#对象时,理解对象的字段和构造函数的顺序是非常重要的:
- Derived static fields//派生类静态字段
- Derived static constructor//派生来静态构造函数
- Derived instance fields//派生类实例字段
- Base static fields//基类静态字段
- Base static constructor//基类静态构造函数
- Base instance fields//基类实例字段
- Base instance constructor//基类实例构造函数
- Derived instance constructor//派生类实例构造函数
1using System;
2
3namespace 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 }
55 }
56 class Tracker
57 {
58 public Tracker( string text )
59 {
60 Console.WriteLine( text );
61 }
62 }
63}
64
65下面是程序执行结果:
2
3namespace 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 }
55 }
56 class Tracker
57 {
58 public Tracker( string text )
59 {
60 Console.WriteLine( text );
61 }
62 }
63}
64
65下面是程序执行结果:
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
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. //避免从构造函数中调用虚拟方法.