Initialize reference type static fields inline

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

 

TypeName

InitializeReferenceTypeStaticFieldsInline

CheckId

CA1810

Category

Microsoft.Performance

Breaking Change

NonBreaking

Cause

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.

Example

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", _
            System.Reflection.Assembly.GetExecutingAssembly())
         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", _
            System.Reflection.Assembly.GetExecutingAssembly())
         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.

Output

.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

gcc 编译器对 inline 函数的支持

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

关于头文件中的 static inline函数

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

深入理解 ButterKnife,让你的程序学会写代码

0、引子 话说我们做程序员的,都应该多少是个懒人,我们总是想办法驱使我们的电脑帮我们干活,所以我们学会了各式各样的语言来告诉电脑该做什么——尽管,他们有时候也会误会我们的意思。 突然有一天,我觉得...
  • f2006116
  • f2006116
  • 2016年07月15日 21:57
  • 1172

Kotlin Reference (九) Properties and Fields

most from reference声明属性Koltin的类都有属性,这些属性可以声明为可变的,使用var关键字或用val关键字生声明不可变属性。class Address { var na...
  • shineflowers
  • shineflowers
  • 2017年08月07日 10:22
  • 154

Reference Pivot Fields and Pivot Items in an Excel Pivot Table, using VBA

This section explains how to access, reference, select or use Pivot Fields and Pivot Items in a Pivo...
  • compassbutton
  • compassbutton
  • 2015年05月25日 16:35
  • 1701

单例中涉及的static,load,initialize

单例模式:懒汉式:第一次用到单例对象时,在创建(适用移动端)   饿汉式:一进入程序就创建一个单例对象:(在load方法里,alloc单例对象) 单例:1. 创建唯一的单例对象:static修饰全局变...
  • liuyixia01220
  • liuyixia01220
  • 2015年09月10日 16:01
  • 249

使用Objective-C的+(void)initialize初始化static变量

在《Objective C类方法load和initialize的区别》一文中,我介绍了Objective-C对待+(void)initialize和+(void)load两个方法在编译和执行时出现的不...
  • y_csdnblog_xx
  • y_csdnblog_xx
  • 2016年06月02日 09:47
  • 262

Professional JS(三-statements&function)&(四-primitive&reference type)&黑画(5-another road)

一.Statements---flow control statements 1.if语句 ①if(condition)statement1 else statement 2.do wh...
  • m0_37851778
  • m0_37851778
  • 2017年07月05日 13:56
  • 77

Caused by: java.lang.AssertionError: illegal type variable reference 解决办法

07-24 10:54:13.529: E/AndroidRuntime(12130): FATAL EXCEPTION: AsyncTask #3 07-24 10:54:13.529: E/And...
  • Bobbybear
  • Bobbybear
  • 2013年08月13日 18:14
  • 3502

iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Type

值和引用类型 Value and Reference Types 在Swift中,有两种数据类型. 一是"值类型"(value type), 它是每一个实例都保存有各自的数据,通常定义为struct...
  • yao_yu_126
  • yao_yu_126
  • 2014年08月17日 16:03
  • 1177
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Initialize reference type static fields inline
举报原因:
原因补充:

(最多只允许输入30个字)