C # as a purely object-oriented word, the whole code written for it everywhere can not be separated from the object. The complete lifecycle of an object is from the beginning to the allocation of space to initialization, to use, and finally to destroy, the use of the resource is reclaimed. To really write high quality code, we have to be a state of each phase of the period, what the framework has done, and what we can do to have some understanding.
In general, most programmers for a created object how to use is more clear, so this article also does not want to do too much of this part of the description, focus on the creation and destruction of the two phases of the object, which is the most easily the programmer error order. In this paper, we first talk about the initialization of object members, as for the release and destruction of objects, I would like to put it in another article to say. Although this article takes c#2005 as an example, it should, by extension, be the same for other CLS-compliant languages.
First, let's look at the member initialization process for the reference type
Let's take a look at an example.
1class Program
2{
3 static void Main(string[] args)
4 {
5 DriveB d = new DriveB();
6 }
7}
8
9class BaseA
10{
11 static DisplayClass a = new DisplayClass("基类静态成员初始化");
12
13 DisplayClass BaseA_c = new DisplayClass("基类实例变量BaseA_c初始化");
14
15 public BaseA()
16 {
17 Console.WriteLine("基类构造方法被调用");
18 }
19}
20
21class DriveB : BaseA
22{
23 static DisplayClass DriveB_b = new DisplayClass("继承类静态成员DriveB_b初始化");
24
25 //static BaseA DriveB_a = new BaseA();
26
27 DisplayClass DriveB_c = new DisplayClass("继承类实例变量DriveB_c初始化");
28
29 public DriveB()
30 {
31 Console.WriteLine("继承类构造方法被调用");
32 }
33}
34class DisplayClass
35{
36 public DisplayClass(string diplayString)
37 {
38 Console.WriteLine(diplayString);
39 Console.WriteLine();
40 }
41}