diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1822.md b/docs/fundamentals/code-analysis/quality-rules/ca1822.md index fc63b9fc548af..5091b39f79a92 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1822.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1822.md @@ -10,6 +10,8 @@ helpviewer_keywords: - CA1822 author: gewarren ms.author: gewarren +dev_langs: +- CSharp --- # CA1822: Mark members as static @@ -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. diff --git a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1822.cs b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1822.cs new file mode 100644 index 0000000000000..c0f81578b44e7 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1822.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; + +namespace ca1822 +{ + // + public class Printer + { + private readonly List _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"); + } + } + // +}