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

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - YQM

NUnit单元测试属性介绍 没有使用lock的Singleton How to serialize and deserialize using C# .NET - YQM Javascript调用web service 简单的英语不简单 我们应该怎样去学习和工作--写给自己 扩展方法 使用反射-动态创建对象及调用对象方法 使用XmlWriter对象 使用XmlReader类 - YQM 知道做什么而不是怎样做(摘抄) 生活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,是因为第一次引用静态成员引发了静态构造函数……

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