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/ca1822.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ helpviewer_keywords:
- CA1822
author: gewarren
ms.author: gewarren
dev_langs:
- CSharp
---
# CA1822: Mark members as static

Expand All @@ -33,6 +35,10 @@ Members that do not access instance data or call instance methods can be marked

Mark the member as static (or Shared in Visual Basic) or use 'this'/'Me' in the method body, if appropriate.

## Example

:::code language="csharp" source="snippets/csharp/all-rules/ca1822.cs" id="snippet1":::

## When to suppress warnings

It is safe to suppress a warning from this rule for previously shipped code for which the fix would be a breaking change.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;

namespace ca1822
{
//<snippet1>
public class Printer
{
private readonly List<char> _items = [
'H', 'e', 'l', 'l', 'o',
];

public void PrintHello()
{
BadPrintHelloInternal();
GoodPrintHelloInternal();
GoodPrintHelloStaticInternal();
}

// This method violates the rule.
private void BadPrintHelloInternal()
{
Console.WriteLine("Hello");
}

// This methods satisfies the rule.
private void GoodPrintHelloInternal()
{
Console.WriteLine(string.Join(string.Empty, this._items));
}

private static void GoodPrintHelloStaticInternal()
{
Console.WriteLine("Hello");
}
}
//</snippet1>
}