CA2207: Initialize value type static fields inline

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName InitializeValueTypeStaticFieldsInline
CheckId CA2207
Category Microsoft.Usage
Breaking Change Non Breaking

Cause

A value-type declares an explicit static constructor.

Rule Description

When a value-type is declared, it undergoes a default initialization where all value-type fields are set to zero and all reference-type fields are set to null (Nothing in Visual Basic). An explicit static constructor is only guaranteed to run before an instance constructor or static member of the type is called. Therefore, if the type is created without calling an instance constructor, the static constructor is not guaranteed to run.

If all static data is initialized inline and no explicit static constructor is declared, the C# and Visual Basic compilers add the beforefieldinit flag to the MSIL class definition. The compilers also add a private static constructor that contains the static initialization code. This private static constructor is guaranteed to run before any static fields of the type are accessed.

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 Suppress Warnings

Do not suppress a warning from this rule.

CA1810: Initialize reference type static fields inline