Initialize reference type static fields inline

转载 2006年05月26日 15:18:00
Visual Studio Team System 
Initialize reference type static fields inline 








Breaking Change



A reference type declares an explicit static constructor.

Rule Description

When a type declares an explicit static constructor, the just-in-time (JIT) compiler adds a check to each of the type's static methods and instance constructors to ensure that the static constructor was previously called. Static initialization is triggered when any static member is accessed or when an instance of the type is created. However, static initialization is not triggered if you declare a variable of the type but do not use it, which can be important if the initialization changes global state.

When all static data is initialized inline and an explicit static constructor is not declared, Microsoft intermediate language (MSIL) compilers add the beforefieldinit flag and an implicit static constructor, which initializes the static data, to the MSIL type definition. When the JIT compiler encounters the beforefieldinit flag, in most cases the static constructor checks are not added. Static initialization is guaranteed to occur at some time before any of the static fields are accessed but not before a static method or instance constructor is invoked. Note that static initialization can occur at any time after a variable of the type is declared.

Static constructor checks can reduce performance. Often a static constructor is used only to initialize static fields, in which case it is only necessary to ensure that static initialization occurs before the first access of a static field. The beforefieldinit behavior is appropriate for these and most other types. It is only inappropriate when static initialization affects global state and one of the following is true:

  • The affect on global state is expensive and is not needed if the type is not used.

  • The global state effects can be accessed without accessing any static fields of the type.

How to Fix Violations

To fix a violation of this rule, initialize all static data when it is declared and remove the static constructor.

When to Exclude Warnings

It is safe to exclude a warning from this rule if performance is not a concern; or if global state changes due to static initialization are expensive, or must be guaranteed to occur before a static method of the type is called or an instance of the type is created.


The following example shows a type, StaticConstructor, that violates the rule and a type, NoStaticConstructor, that replaces the static constructor with inline initialization to satisfy the rule.

Visual Basic Copy Code
Imports System
Imports System.Resources

Namespace PerformanceLibrary

   Public Class StaticConstructor

      Shared someInteger As Integer
      Shared resourceString As String 

      Shared Sub New()

         someInteger = 3
         Dim stringManager As New ResourceManager("strings", _
         resourceString = stringManager.GetString("string")

      End Sub

   End Class

   Public Class NoStaticConstructor

      Shared someInteger As Integer = 3
      Shared resourceString As String = InitializeResourceString()

      Shared Private Function InitializeResourceString()

         Dim stringManager As New ResourceManager("strings", _
         Return stringManager.GetString("string")

      End Function

   End Class

End Namespace
using System;
using System.Reflection;
using System.Resources;

namespace PerformanceLibrary
   public class StaticConstructor
      static int someInteger;
      static string resourceString;

      static StaticConstructor()
         someInteger = 3;
         ResourceManager stringManager = 
            new ResourceManager("strings", Assembly.GetExecutingAssembly());
         resourceString = stringManager.GetString("string");

   public class NoStaticConstructor
      static int someInteger = 3;
      static string resourceString = InitializeResourceString();

      static string InitializeResourceString()
         ResourceManager stringManager = 
            new ResourceManager("strings", Assembly.GetExecutingAssembly());
         return stringManager.GetString("string");

Note the addition of the beforefieldinit flag on the MSIL definition for the NoStaticConstructor class.


.class public auto ansi StaticConstructor
       extends [mscorlib]System.Object
} // end of class StaticConstructor

.class public auto ansi beforefieldinit NoStaticConstructor
       extends [mscorlib]System.Object
} // end of class NoStaticConstructor
Related Rules

Android studio Inspection 优化笔记

以下内容皆为基于百度后一些内容整合,若有不对之处,请大家多多指教。 Android > Lint > Performance  1、HashMap can be replaced with Sp...
  • wxm1225929690
  • wxm1225929690
  • 2016年11月16日 10:25
  • 3528

头文件中的static inline

  • maoxunxing
  • maoxunxing
  • 2014年11月24日 13:07
  • 1296

gcc inline理解

只测试过Ubuntu gcc,其他未测试。 1、inline 函数 在Ubuntu 16.04.2 LTS 上gcc无法编译通过,显示undefined reference to `print',...
  • u012489250
  • u012489250
  • 2017年06月02日 15:32
  • 333

gcc 编译器对 inline 函数的支持

C99版的C语言引入了inline关键字开始支持inline 函数,在这之前传统的C语言(C89)是没有inline 关键字的,也不支持inline 函数。不过大多数 C89 的编译器都将inline...
  • liyuanbhu
  • liyuanbhu
  • 2015年01月06日 22:25
  • 5581

泛型类与静态泛型方法 Cannot make a static reference to the non-static type

class ToList2 { public static ToList create(E...ts) { ToList myList = new ToList(); myLi...
  • ruan130
  • ruan130
  • 2014年04月13日 16:58
  • 1511

php static function

:: class Test{ public static $test = 1; public static function test(){ } } 可以不用实例化对象直接使用 Test:...
  • zxc7928932
  • zxc7928932
  • 2015年03月18日 22:01
  • 181

从Masonry 源码探 Objective-C [源码学习开篇]

前言: 在继续叨叨, Masonry 是一个关于iOS开发的布局框架, Masonry是对NSLayoutConstraint 的封装, 你知道的NSLayoutConstraint这个家伙使用起来很...
  • sinat_30162391
  • sinat_30162391
  • 2016年11月24日 15:32
  • 782

关于 inline 函数的坑: *** undefined reference to ***

如果将函数的实现放在头文件中,那么每一个包含该头文件的cpp文件都将得到一份关于该函数的定义,那么编译器会报函数重定义错误。 如果将函数的实现放在头文件,并且标记为 inline 那么每一个包含该头文...
  • GW569453350game
  • GW569453350game
  • 2017年09月11日 16:25
  • 426

关于头文件中的 static inline函数

关于头文件中的 static inline函数     头文件中常见static inline函数,于是思考有可能遇到的问题,如头文件经常会被包含会不会产生很多副本?网上说法不一。于是自己验证。经过...
  • huanghui167
  • huanghui167
  • 2014年11月21日 11:48
  • 18414


日期:2015年4月17日 15:44 星期五 农历 乙未 羊年 二月廿九 修改记录: 正文: inline与static这两个关键字经常遇见,而且遇见它的人大多都以为自己完全掌握了,其...
  • gande08
  • gande08
  • 2015年06月28日 18:09
  • 548
您举报文章:Initialize reference type static fields inline