C#构造函数、静态构造函数、析构函数的用法
using System;
class Test2
{
static int i;
static Test2() { // a constructor for the entire class called
//once before the first object created
i = 4;
Console.Out.WriteLine("inside static construtor...");
}
public Test2() {
Console.Out.WriteLine("inside regular construtor... i={0}",i);
}
~Test2() { // destructor (hopefully) called for each object
Console.Out.WriteLine("inside destructor");
}
static void Main(string[] args)
{
Console.Out.WriteLine("Test2");
new Test2();
new Test2();
}
}
输出结果
inside static construtor... Test2 inside regular construtor... i=4 inside regular construtor... i=4 inside destructor inside destructor
