C#静态构造函数调用机制
来源:互联网 发布:淘宝中差评回复语 编辑:程序博客网 时间:2024/06/01 08:14
若一个类中有静态构造函数,在首次实例化该类或任何的静态成员被引用时,.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(); } }
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(); } }
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(); } }
4.引用静态成员时调用构造函数(将主函数移到类外面)
注释掉obj1和obj2的实例化代码
class Program { static void Main(string[] args) { //StaticConstructorTest obj1 = new StaticConstructorTest(); //StaticConstructorTest obj2 = new StaticConstructorTest(); Console.WriteLine(StaticConstructorTest.Count); Console.ReadKey(); } }
文件下载:https://pan.baidu.com/s/1qYQ2TqO
参考:
C#静态构造函数的调用问题
0 0
- C#静态构造函数调用机制
- C#静态构造函数的调用问题
- C# 静态构造函数
- C#静态构造函数
- C# 静态构造函数
- C# 静态构造函数
- C#静态构造函数
- C#静态构造函数
- C#静态构造函数
- C#静态构造函数
- c#静态构造函数
- C#静态构造函数
- C# 静态构造函数
- C#静态构造函数
- C# 静态构造函数
- C# 静态构造函数
- C#静态构造函数
- c#静态构造函数
- Kubernetes Eviction Manager源码分析
- 基类与派生类的内部存储细节
- Alpha测试与Beta测试概念
- 泛型模型实例
- 通量与散度
- C#静态构造函数调用机制
- Zookeeper与Paxos
- 火影忍者启航篇
- angularJS+requireJS实现controller及directive的按需加载
- CreateFile, ReadFile详解
- 集合框架
- mysql开远程访问权限
- 20pin J-link使用SWD模式连接蓝宙K60DN512核心板
- throws和throw以及Finally的用法