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

推荐订阅源

博客园 - 【当耐特】
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
Schneier on Security
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Last Watchdog
The Last Watchdog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
News and Events Feed by Topic
P
Proofpoint News Feed
H
Heimdal Security Blog
云风的 BLOG
云风的 BLOG
H
Hacker News: Front Page
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
F
Full Disclosure
小众软件
小众软件
Martin Fowler
Martin Fowler
Security Latest
Security Latest
The Cloudflare Blog
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
I
InfoQ
AI
AI
Blog — PlanetScale
Blog — PlanetScale
I
Intezer
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
V
Vulnerabilities – Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
V2EX
N
News and Events Feed by Topic
C
Cybersecurity and Infrastructure Security Agency CISA
T
Troy Hunt's Blog
大猫的无限游戏
大猫的无限游戏
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Register - Security
The Register - Security
Forbes - Security
Forbes - Security
Recent Announcements
Recent Announcements
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
S
SegmentFault 最新的问题
S
Securelist
Cloudbric
Cloudbric
T
Tenable Blog
D
Docker

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

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