diff --git a/docs/fundamentals/code-analysis/quality-rules/ca2227.md b/docs/fundamentals/code-analysis/quality-rules/ca2227.md index 47cbbd56a0226..3af1a32a185fc 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca2227.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca2227.md @@ -1,7 +1,7 @@ --- title: "CA2227: Collection properties should be read only (code analysis)" description: "Learn about code analysis rule CA2227: Collection properties should be read only" -ms.date: 09/28/2018 +ms.date: 07/08/2021 ms.topic: reference f1_keywords: - CA2227 @@ -29,13 +29,13 @@ An externally visible, writable property is of a type that implements and methods of the class for an example of this pattern. +A writable collection property allows a user to replace the collection with a completely different collection. A read-only or [init-only](../../../csharp/language-reference/keywords/init.md) property stops the collection from being replaced, but still allows the individual members to be set. If replacing the collection is a goal, the preferred design pattern is to include a method to remove all the elements from the collection, and a method to repopulate the collection. See the and methods of the class for an example of this pattern. Both binary and XML serialization support read-only properties that are collections. The class has specific requirements for types that implement and in order to be serializable. ## How to fix violations -To fix a violation of this rule, make the property read-only. If the design requires it, add methods to clear and repopulate the collection. +To fix a violation of this rule, make the property read-only or [init-only](../../../csharp/language-reference/keywords/init.md). If the design requires it, add methods to clear and repopulate the collection. ## When to suppress warnings @@ -54,3 +54,7 @@ The following example shows a type with a writable collection property and shows ## Related rules - [CA1819: Properties should not return arrays](ca1819.md) + +## See also + +- [Properties in C#](../../../csharp/programming-guide/classes-and-structs/properties.md) diff --git a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2227.cs b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2227.cs index 2deabd1b58fc9..d1752afa5a0b9 100644 --- a/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2227.cs +++ b/docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca2227.cs @@ -11,7 +11,7 @@ public ArrayList SomeStrings get; // This set accessor violates rule CA2227. - // To fix the code, remove this set accessor. + // To fix the code, remove this set accessor or change it to init. set; }