c# 对象的创建过程
1:分配静态成员的内存空间,值类型为0,引用类型为null
2: 执行静态成员的赋值。
3:执行静态的构造函数。
4:分配成员的内存空间,值为0,引用类型为null
5:执行成员的赋值。
6:分配基类静态成员的内存空间,值为0,引用类型为null
7:执行基类静态成员的赋值。
8:执行基类的静态构造函数。
9:基类的成员的声明和赋值。
10:基类的构造函数
11:构造函数
注:静态的成员和构筑函数只运行一次。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Program
{
static void Main(string[] args)
{
ClB b = new ClB();
b = new ClB();
Console.ReadLine();
}
}
public class ClsA
{
private static readonly string strA = "A";
private string name;
public string Name
{
set { this.name = value; }
get{return name;}
}
static ClsA()
{
Console.WriteLine(strA);
strA = "A Changed";
Console.WriteLine(strA);
Console.WriteLine("A staic constructor!");
}
public ClsA()
{
Console.WriteLine("A constructor!");
}
}
public class ClB:ClsA
{
private static readonly string strB = "B";
private int age =12;
static ClB()
{
Console.WriteLine(strB);
strB = "B Changed!";
Console.WriteLine(strB);
Console.WriteLine("B static constructor!");
}
public ClB()
{
Console.WriteLine(age);
Console.WriteLine("B constructor!");
}
}
{
static void Main(string[] args)
{
ClB b = new ClB();
b = new ClB();
Console.ReadLine();
}
}
public class ClsA
{
private static readonly string strA = "A";
private string name;
public string Name
{
set { this.name = value; }
get{return name;}
}
static ClsA()
{
Console.WriteLine(strA);
strA = "A Changed";
Console.WriteLine(strA);
Console.WriteLine("A staic constructor!");
}
public ClsA()
{
Console.WriteLine("A constructor!");
}
}
public class ClB:ClsA
{
private static readonly string strB = "B";
private int age =12;
static ClB()
{
Console.WriteLine(strB);
strB = "B Changed!";
Console.WriteLine(strB);
Console.WriteLine("B static constructor!");
}
public ClB()
{
Console.WriteLine(age);
Console.WriteLine("B constructor!");
}
}
学习,积累中......