From 88a783d1f314d25e562b07218d86dab09842b0c4 Mon Sep 17 00:00:00 2001 From: kurnakovv Date: Wed, 8 Oct 2025 15:59:05 +0900 Subject: [PATCH 1/2] Add code example for CA1822 rule (#48964) --- .../code-analysis/quality-rules/ca1822.md | 6 ++++ .../snippets/csharp/all-rules/ca1822.cs | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1822.cs 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..54367d0c2c958 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1822.cs @@ -0,0 +1,36 @@ +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"); + } + } +} From c40915e5a43ddff8b84b93ae051860dffd6350f9 Mon Sep 17 00:00:00 2001 From: kurnakovv Date: Wed, 8 Oct 2025 16:03:33 +0900 Subject: [PATCH 2/2] Add snippet for CA1822 (#48964) --- .../quality-rules/snippets/csharp/all-rules/ca1822.cs | 2 ++ 1 file changed, 2 insertions(+) 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 index 54367d0c2c958..c0f81578b44e7 100644 --- 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 @@ -3,6 +3,7 @@ namespace ca1822 { + // public class Printer { private readonly List _items = [ @@ -33,4 +34,5 @@ private static void GoodPrintHelloStaticInternal() Console.WriteLine("Hello"); } } + // }