Skip to content
Merged
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
10 changes: 7 additions & 3 deletions docs/fundamentals/code-analysis/quality-rules/ca2227.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -29,13 +29,13 @@ An externally visible, writable property is of a type that implements <xref:Syst

## Rule description

A writable collection property allows a user to replace the collection with a completely different collection. A read-only 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 <xref:System.Collections.ArrayList.Clear%2A> and <xref:System.Collections.ArrayList.AddRange%2A> methods of the <xref:System.Collections.ArrayList?displayProperty=fullName> 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 <xref:System.Collections.ArrayList.Clear%2A> and <xref:System.Collections.ArrayList.AddRange%2A> methods of the <xref:System.Collections.ArrayList?displayProperty=fullName> class for an example of this pattern.

Both binary and XML serialization support read-only properties that are collections. The <xref:System.Xml.Serialization.XmlSerializer?displayProperty=fullName> class has specific requirements for types that implement <xref:System.Collections.ICollection> and <xref:System.Collections.IEnumerable?displayProperty=fullName> 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

Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down