From a9cc889b177fd4ab7a55d5615f402586d18d57b3 Mon Sep 17 00:00:00 2001 From: kurnakovv Date: Fri, 10 Oct 2025 15:52:10 +0900 Subject: [PATCH 1/2] Add code example for CA2207 rule (#49060) --- .../code-analysis/quality-rules/ca2207.md | 6 ++++ .../snippets/csharp/all-rules/ca2207.cs | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2207.cs 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..ec0d7021424e0 --- /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; + + // ... + } + // +} \ No newline at end of file From 0f7baba35d7767a4c8ad3376ffc8a588ffc4791a Mon Sep 17 00:00:00 2001 From: kurnakovv Date: Fri, 10 Oct 2025 15:53:13 +0900 Subject: [PATCH 2/2] Add blank line to end (#49060) --- .../quality-rules/snippets/csharp/all-rules/ca2207.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index ec0d7021424e0..54346031ba154 100644 --- 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 @@ -25,4 +25,4 @@ struct GoodStruct // ... } // -} \ No newline at end of file +}