Today I read a post from Eric Lipert that describes the harm of arrays. It´s mentioned that when we need a collection of values we should provide values, not a variable that points to a list of values. Thus Eric suggests that whenever we want to return a collection of items in a method, we should return an
We could of course wrap the collection into a
ReadOnlyCollectionand return an
Why do that?
IList<T>, so unless there's some better approach, declaring a return type of
IList<T> and returning an instance of
ReadOnlyCollection<T> seems like a good way to go.
However, it just so happens that in current .NET Framework versions, there is a slightly better way: return an instance of
ReadOnlyCollection<T>, but specify a return type of
IList<T> doesn't really promise to allow modification by the caller,
IReadOnlyList<T> is explicit about the intent.