Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca2207.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ helpviewer_keywords:
- "InitializeValueTypeStaticFieldsInline"
author: gewarren
ms.author: gewarren
dev_langs:
- CSharp
---
# CA2207: Initialize value type static fields inline

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace ca2207
{
//<snippet1>
// 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;

// ...
}
//</snippet1>
}