Skip to content

Commit eae69e4

Browse files
authored
Add code example for CA1823 rule (#48966) (#48967)
1 parent ed27215 commit eae69e4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ helpviewer_keywords:
1010
- CA1823
1111
author: gewarren
1212
ms.author: gewarren
13+
dev_langs:
14+
- CSharp
1315
---
1416
# CA1823: Avoid unused private fields
1517

@@ -33,6 +35,10 @@ Private fields were detected that do not appear to be accessed in the assembly.
3335

3436
To fix a violation of this rule, remove the field or add code that uses it.
3537

38+
## Example
39+
40+
:::code language="csharp" source="snippets/csharp/all-rules/ca1823.cs" id="snippet1":::
41+
3642
## When to suppress warnings
3743

3844
It is safe to suppress a warning from this rule.
@@ -60,3 +66,4 @@ For more information, see [How to suppress code analysis warnings](../suppress-w
6066

6167
- [CA1812: Avoid uninstantiated internal classes](ca1812.md)
6268
- [CA1801: Review unused parameters](ca1801.md)
69+
- [Remove unread private member (IDE0052)](../style-rules/ide0052.md)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace ca1823
2+
{
3+
//<snippet1>
4+
public class User
5+
{
6+
private readonly string _firstName;
7+
private readonly string _lastName;
8+
9+
// CA1823: Unused field '_age'
10+
private readonly int _age;
11+
12+
public User(string firstName, string lastName)
13+
{
14+
_firstName = firstName;
15+
_lastName = lastName;
16+
}
17+
18+
public string GetFullName()
19+
{
20+
return $"My name is {_firstName} {_lastName}";
21+
}
22+
}
23+
//</snippet1>
24+
}

0 commit comments

Comments
 (0)