惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Security Latest
Security Latest
S
Secure Thoughts
H
Heimdal Security Blog
The Last Watchdog
The Last Watchdog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
PCI Perspectives
PCI Perspectives
C
CERT Recently Published Vulnerability Notes
C
CXSECURITY Database RSS Feed - CXSecurity.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
爱范儿
爱范儿
腾讯CDC
P
Privacy & Cybersecurity Law Blog
量子位
T
Threat Research - Cisco Blogs
V
V2EX
S
Schneier on Security
P
Proofpoint News Feed
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
月光博客
月光博客
C
Cybersecurity and Infrastructure Security Agency CISA
Schneier on Security
Schneier on Security
L
LINUX DO - 最新话题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
S
SegmentFault 最新的问题
G
GRAHAM CLULEY
人人都是产品经理
人人都是产品经理
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tenable Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - YQM

NUnit单元测试属性介绍 没有使用lock的Singleton How to serialize and deserialize using C# .NET - YQM Javascript调用web service 简单的英语不简单 我们应该怎样去学习和工作--写给自己 扩展方法 使用反射-动态创建对象及调用对象方法 使用XmlWriter对象 使用XmlReader类 知道做什么而不是怎样做(摘抄) 生活15点,要记住! 设计模式之Command模式 使用javascript动态添加和删除table的行和列 Generate unique strings and numbers in C#(生成一个唯一的字符串和数值) Some notes Localization in ASP.NET 2.0 ASP.NET 2.0: Playing a bit with GridView "Sort Grouping" 通过TryParse来检验和转换数据类型
C#静态构造函数
YQM · 2008-09-18 · via 博客园 - YQM

MSDN的定义:静态构造函数用于初始化任何静态数据,或用于执行仅需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数

背后隐藏不少东西,看代码:

 1    class Program
 2    {
 3        static void Main(string[] args)
 4        {
 5            Class1 o1 = new Class1();
 6            Class1 o2 = new Class1();
 7            Console.WriteLine(Class1.Count);
 8        }

 9    }

10
11    class Class1
12    {
13        public static int Count = 0;
14
15        static Class1()
16        {
17            Count++;
18        }

19
20        public Class1()
21        {
22            Count++;
23        }

24
25    }

 结果Class1.Count=3,

正如定义说的,在创建第一个实例的时候会值执行静态构造函数,执行后count=1,要点是,静态构造函数就想一个辅助的东西一样,在最先执行完静态构造函数后类的构造函数依然执行,所以这是count++后,count=2了,再次创建实例o2,这时已经是创建第二个实例了,静态构造函数不再执行,所以只执行构造函数,那么count++后,count=3了。

再看下引用静态成员:

 1    class Program
 2    {
 3        static void Main(string[] args)
 4        {
 5            Console.WriteLine(Class1.Count);
 6            Class1 o1 = new Class1();
 7            Class1 o2 = new Class1();
 8            Console.WriteLine(Class1.Count);
 9        }

10    }

11
12    class Class1
13    {
14        public static int Count = 0;
15
16        static Class1()
17        {
18            Count++;
19        }

20
21        public Class1()
22        {
23            Count++;
24        }

25
26    }

程序输出1,3

输出1,是因为第一次引用静态成员引发了静态构造函数……

最后,记住一点:类的静态构造函数在给定应用程序域中至多执行一次,只有创建类的实例或者引用类的任何静态成员才引发静态构造函数