C#中的BeforeFieldInit

本文探讨了C#中的BeforeFieldInit标志,它影响静态构造函数的执行时机。当类没有静态构造函数时,会被标记为BeforeFieldInit,意味着类型初始化可在首次访问静态字段之前或任何时候进行。通过示例展示了With和Without静态构造函数时的不同行为,强调了BeforeFieldInit可能导致的早期初始化情况。
摘要由CSDN通过智能技术生成

今天学习设计模式中的单例模式,无意间发现了这个标志BeforeFieldInit

于是简单地搜索了一下,总结出如下内容。

The C# specification states:

The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:
* An instance of the class is created.
* Any of the static members of the class are referenced.

The CLI specification (ECMA 335) states in section 8.9.5:

  1. A type may have a type-initializer method, or not.
  2. A type may be specified as having a relaxed semantic for its type-initializer method (for convenience below, we call this relaxed semantic BeforeFieldInit)
  3. If marked BeforeFieldInit then the type’s initializer method is executed at, or sometime before, first access to any static field defined for that type
  4. If not marked Befor
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值