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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
爱范儿
爱范儿
GbyAI
GbyAI
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Secure Thoughts
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
罗磊的独立博客
Y
Y Combinator Blog
Know Your Adversary
Know Your Adversary
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recorded Future
Recorded Future
S
Securelist
T
Tor Project blog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
P
Privacy & Cybersecurity Law Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Project Zero
Project Zero
T
Tailwind CSS Blog
腾讯CDC
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
The Hacker News
The Hacker News
F
Full Disclosure
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
美团技术团队
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
C
Cybersecurity and Infrastructure Security Agency CISA
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
O
OpenAI News
Cloudbric
Cloudbric

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

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