Skip to content

Commit fbb1cbd

Browse files
maikelstenekermapfel
authored andcommitted
Remove ICollection<T> as an allowed interface from AV1130 (dennisdoomen#216)
Reasoning: the ICollection<T> interface actually allows one to modify the collection, which goes against the intention of this guideline. The reason for this exception was that versions of .NET Framework before 4.5 did not include immutable collections. Closes dennisdoomen#215
1 parent c8c7ea5 commit fbb1cbd

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

_rules/1130.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
---
22
rule_id: 1130
33
rule_category: member-design
4-
title: Return an `IEnumerable<T>` or `ICollection<T>` instead of a concrete collection class
4+
title: Return interfaces to unchangeable collections
55
severity: 2
66
---
7-
You generally don't want callers to be able to change an internal collection, so don't return arrays, lists or other collection classes directly. Instead, return an `IEnumerable<T>`, or, if the caller must be able to determine the count, an `ICollection<T>`.
8-
9-
**Note:** If you're using .NET 4.5 or higher, you can also use `IReadOnlyCollection<T>`, `IReadOnlyList<T>` or `IReadOnlyDictionary<TKey, TValue>`.
7+
You generally don't want callers to be able to change an internal collection, so don't return arrays, lists or other collection classes directly. Instead, return an `IEnumerable<T>`, `IReadOnlyCollection<T>`, `IReadOnlyList<T>` or `IReadOnlyDictionary<TKey, TValue>`.
108

119
**Exception:** Immutable collections such as `ImmutableArray<T>`, `ImmutableList<T>` and `ImmutableDictionary<TKey, TValue>` prevent modifications from the outside and are thus allowed.

0 commit comments

Comments
 (0)