From 2e717d62ff2e35cef28732e251c6e5ffd182d2f7 Mon Sep 17 00:00:00 2001 From: kurnakovv Date: Sat, 27 Sep 2025 15:41:17 +0900 Subject: [PATCH 1/3] Add code example for CA1002 rule (#48762) --- .../code-analysis/quality-rules/ca1002.md | 3 ++ .../snippets/csharp/all-rules/ca1002.cs | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1002.md b/docs/fundamentals/code-analysis/quality-rules/ca1002.md index e7ab8e82def88..10ed77ac5dd66 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1002.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1002.md @@ -45,6 +45,9 @@ By default, this rule only looks at externally visible types, but this is [confi To fix a violation of this rule, change the type to one of the generic collections that's designed for inheritance. +## Example +:::code language="csharp" source="snippets/csharp/all-rules/ca1002.cs" id="snippet1"::: + ## When to suppress warnings Do not suppress a warning from this rule unless the assembly that raises this warning is not meant to be a reusable library. For example, it would be safe to suppress this warning in a performance-tuned application where a performance benefit was gained from the use of generic lists. diff --git a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs new file mode 100644 index 0000000000000..4e64f2410185f --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; + +namespace ca1001 +{ + // + // This class violates the rule. + public class MutableItems + { + // CA1002: Change 'List' in 'ViolationItems.Members' to use 'Collection', 'ReadOnlyCollection' or 'KeyedCollection' + public List Items { get; } = new List(); + + public void Add(string item) + { + Items.Add(item); + } + } + + // This class satisfies the rule. + public class ReadOnlyItems + { + private readonly List _items = new List(); + + public IReadOnlyCollection Items => _items.AsReadOnly(); + + public void Add(string item) + { + _items.Add(item); + } + } + // +} From f83f06a03ea3da4f7349b3ab97b5049465355d22 Mon Sep 17 00:00:00 2001 From: kurnakovv Date: Tue, 30 Sep 2025 23:09:18 +0900 Subject: [PATCH 2/3] Fix review threads (#48762) --- docs/fundamentals/code-analysis/quality-rules/ca1002.md | 3 +++ .../quality-rules/snippets/csharp/all-rules/ca1002.cs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1002.md b/docs/fundamentals/code-analysis/quality-rules/ca1002.md index 10ed77ac5dd66..2cbd1731aa77d 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1002.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1002.md @@ -10,6 +10,8 @@ helpviewer_keywords: - DoNotExposeGenericLists author: gewarren ms.author: gewarren +dev_langs: + - CSharp --- # CA1002: Do not expose generic lists @@ -46,6 +48,7 @@ By default, this rule only looks at externally visible types, but this is [confi To fix a violation of this rule, change the type to one of the generic collections that's designed for inheritance. ## Example + :::code language="csharp" source="snippets/csharp/all-rules/ca1002.cs" id="snippet1"::: ## When to suppress warnings diff --git a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs index 4e64f2410185f..4865025759d7e 100644 --- a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs +++ b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs @@ -6,7 +6,7 @@ namespace ca1001 // This class violates the rule. public class MutableItems { - // CA1002: Change 'List' in 'ViolationItems.Members' to use 'Collection', 'ReadOnlyCollection' or 'KeyedCollection' + // CA1002: Change 'List' in 'MutableItems.Items' to use 'Collection', 'ReadOnlyCollection' or 'KeyedCollection' public List Items { get; } = new List(); public void Add(string item) From 2e1cef601ebc82805ff6cb88d28a827a3fa2677d Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Tue, 30 Sep 2025 08:17:20 -0700 Subject: [PATCH 3/3] Break up comment line to ease scrolling --- .../quality-rules/snippets/csharp/all-rules/ca1002.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs index 4865025759d7e..3440b130d48c3 100644 --- a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs +++ b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1002.cs @@ -6,7 +6,8 @@ namespace ca1001 // This class violates the rule. public class MutableItems { - // CA1002: Change 'List' in 'MutableItems.Items' to use 'Collection', 'ReadOnlyCollection' or 'KeyedCollection' + // CA1002: Change 'List' in 'MutableItems.Items' to + // use 'Collection', 'ReadOnlyCollection' or 'KeyedCollection'. public List Items { get; } = new List(); public void Add(string item)