| rule_id | 1130 |
|---|---|
| rule_category | member-design |
| title | Return interfaces to unchangeable collections |
You generally don't want callers to be able to change an internal collection, so don't return arrays, lists or other mutable collection classes directly. Instead, return an IEnumerable<T>, IAsyncEnumerable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, IReadOnlySet<T> or IReadOnlyDictionary<TKey, TValue>.
Be aware that IEnumerable<T> is often perceived as lazy-evaluated. If your collection is already materialized, consider returning IReadOnlyCollection<T> or IReadOnlyList<T> to make the intent clear.
Important
Exception: Immutable collections such as ImmutableArray<T>, ImmutableList<T> and ImmutableDictionary<TKey, TValue>, as well as FrozenSet<T> and FrozenDictionary<TKey, TValue> prevent modifications from the outside and are thus allowed.