diff --git a/docs/fundamentals/code-analysis/quality-rules/ca2207.md b/docs/fundamentals/code-analysis/quality-rules/ca2207.md index 4e2c01f77c5ac..032125401fdc4 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca2207.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca2207.md @@ -10,6 +10,8 @@ helpviewer_keywords: - "InitializeValueTypeStaticFieldsInline" author: gewarren ms.author: gewarren +dev_langs: +- CSharp --- # CA2207: Initialize value type static fields inline @@ -35,6 +37,10 @@ If all static data is initialized inline and no explicit static constructor is d To fix a violation of this rule initialize all static data when it is declared and remove the static constructor. +## Example + +:::code language="csharp" source="snippets/csharp/all-rules/ca2207.cs" id="snippet1"::: + ## When to suppress warnings Do not suppress a warning from this rule. diff --git a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2207.cs b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2207.cs new file mode 100644 index 0000000000000..54346031ba154 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2207.cs @@ -0,0 +1,28 @@ +namespace ca2207 +{ + // + // This struct violates the rule. + struct BadStruct + { + private static readonly int s_first; + private static readonly int s_second; + + static BadStruct() + { + s_first = 1; + s_second = 2; + } + + // ... + } + + // This struct satisfies the rule. + struct GoodStruct + { + private static readonly int s_first = 1; + private static readonly int s_second = 2; + + // ... + } + // +}