C#静态构造函数调用机制

时间:2021-03-06 03:39:07

  若一个类中有静态构造函数,在首次实例化该类或任何的静态成员被引用时,.NET自动调用静态构造函数来初始化该类。注意是“首次”,即继续实例化该类时,不会调用该类的静态构造函数。

1、实例化时调用静态构造函数

    /// <summary>
/// 静态构造函数
/// <remarks>
/// Main主函数在类StaticConstructorTest之中:
///
/// 1.程序被加载时,首先初始化了【静态字段】和【静态构造器】
/// 【1】调用静态字段_count;
/// 【2】调用静态构造函数 static StaticConstructorTest();
/// 【3】调用Main下的obj1代码;
/// </remarks>
/// </summary>
class StaticConstructorTest
{
private static int _count = 0;

static StaticConstructorTest()
{
_count++;
Console.WriteLine(string.Format("static constructor.Count is {0}", _count));
}

StaticConstructorTest()
{
_count++;
Console.WriteLine(string.Format("normal constructor.Count is {0}", _count));
}

static void Main(string[] args)
{
StaticConstructorTest obj1 = new StaticConstructorTest();
StaticConstructorTest obj2 = new StaticConstructorTest();
Console.WriteLine(_count);
Console.ReadKey();
}
}

C#静态构造函数调用机制

2、引用静态成员时调用构造函数

/// <summary>
///
/// 静态构造函数:引用静态成员时调用构造函数
///
/// <remarks>
/// Main主函数在类StaticConstructorTest之中:
///
/// 1.在进入main主函数之前,首先初始化了【静态字段】和【静态构造器】
/// 【1】调用静态字段_count;
/// 【2】调用静态构造函数 static StaticConstructorTest();
///
/// </remarks>
/// </summary>
class StaticConstructorTest
{
private static int _count = 0;

static StaticConstructorTest()
{
_count++;
Console.WriteLine(string.Format("static constructor.Count is {0}", _count));
}

StaticConstructorTest()
{
_count++;
Console.WriteLine(string.Format("normal constructor.Count is {0}", _count));
}

static void Main(string[] args)
{
Console.WriteLine(_count);
Console.ReadKey();
}
}

C#静态构造函数调用机制

3.实例化时调用静态构造函数(将主函数移到类外面)

    /// <summary>
/// 静态构造函数
/// <remarks>
/// 注意:Main主函数在类StaticConstructorTest之外:
///
/// 1. 在 obj1 对象被创建前,StaticConstructorTest 加载静态字段:
/// 【1】调用静态字段_count;
/// 【2】调用静态构造函数 static StaticConstructorTest();
/// 【3】调用构造函数 构建obj1;
///
/// 2.如果注释掉 obj1 和 obj2的构造代码(即:直接访问类的静态属性时,触发初始化【静态字段】和【静态构造器】)
/// 即:直接调用 访问类静态属性Count的代码 Console.WriteLine(StaticConstructorTest.Count),
/// 则调用顺序:
/// 【1】调用静态字段_count;
/// 【2】调用静态构造函数 static StaticConstructorTest();
/// </remarks>
/// </summary>
public class StaticConstructorTest
{
private static int _count = 0;

public static int Count
{
get { return StaticConstructorTest._count; }
}
static StaticConstructorTest() // static ctor
{
_count++;
Console.WriteLine(string.Format("static constructor.Count is {0}", _count));
}
public StaticConstructorTest() // public ctor
{
_count++;
Console.WriteLine(string.Format("normal constructor.Count is {0}", _count));
}
}

class Program
{
static void Main(string[] args)
{
StaticConstructorTest obj1 = new StaticConstructorTest();
StaticConstructorTest obj2 = new StaticConstructorTest();
Console.WriteLine(StaticConstructorTest.Count);
Console.ReadKey();
}
}

C#静态构造函数调用机制

4.引用静态成员时调用构造函数(将主函数移到类外面)

注释掉obj1和obj2的实例化代码

 class Program
{
static void Main(string[] args)
{
//StaticConstructorTest obj1 = new StaticConstructorTest();
//StaticConstructorTest obj2 = new StaticConstructorTest();
Console.WriteLine(StaticConstructorTest.Count);
Console.ReadKey();
}
}

C#静态构造函数调用机制

文件下载:https://pan.baidu.com/s/1qYQ2TqO
参考:
C#静态构造函数的调用问题