Skip to content

Commit 91439ea

Browse files
authored
Add code example for CA2207 rule (#49060) (#49061)
1 parent 0c6575d commit 91439ea

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

docs/fundamentals/code-analysis/quality-rules/ca2207.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ helpviewer_keywords:
1010
- "InitializeValueTypeStaticFieldsInline"
1111
author: gewarren
1212
ms.author: gewarren
13+
dev_langs:
14+
- CSharp
1315
---
1416
# CA2207: Initialize value type static fields inline
1517

@@ -35,6 +37,10 @@ If all static data is initialized inline and no explicit static constructor is d
3537

3638
To fix a violation of this rule initialize all static data when it is declared and remove the static constructor.
3739

40+
## Example
41+
42+
:::code language="csharp" source="snippets/csharp/all-rules/ca2207.cs" id="snippet1":::
43+
3844
## When to suppress warnings
3945

4046
Do not suppress a warning from this rule.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace ca2207
2+
{
3+
//<snippet1>
4+
// This struct violates the rule.
5+
struct BadStruct
6+
{
7+
private static readonly int s_first;
8+
private static readonly int s_second;
9+
10+
static BadStruct()
11+
{
12+
s_first = 1;
13+
s_second = 2;
14+
}
15+
16+
// ...
17+
}
18+
19+
// This struct satisfies the rule.
20+
struct GoodStruct
21+
{
22+
private static readonly int s_first = 1;
23+
private static readonly int s_second = 2;
24+
25+
// ...
26+
}
27+
//</snippet1>
28+
}

0 commit comments

Comments
 (0)