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

推荐订阅源

P
Proofpoint News Feed
V
V2EX
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
Vercel News
Vercel News
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
小众软件
小众软件
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
AWS News Blog
AWS News Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
B
Blog RSS Feed
G
Google Developers Blog
量子位
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
雷峰网
雷峰网
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
F
Full Disclosure
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
The Hacker News
The Hacker News
U
Unit 42
S
SegmentFault 最新的问题
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
罗磊的独立博客
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes

博客园 - 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,是因为第一次引用静态成员引发了静态构造函数……

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