什么时候在 C# 中调用静态构造函数? [英] When is a static constructor called in C#?

查看:15
本文介绍了什么时候在 C# 中调用静态构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一个包含静态构造函数的类时,该构造函数是在包含该类的程序集首次加载时还是在对该类的第一个引用被命中时调用?

When I have class containing a static constructor, is that constructor called when the assembly containing the class is first loaded or when the first reference to that class is hit?

推荐答案

第一次访问类时.

静态构造函数 (C#编程指南)

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

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

这篇关于什么时候在 C# 中调用静态构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆