I found Google C# style guide and these points
For inputs use the most restrictive collection type possible, for example IReadOnlyCollection / IReadOnlyList / IEnumerable as inputs to methods when the inputs should be immutable.
For outputs, if passing ownership of the returned container to the owner, prefer IList over IEnumerable. If not transferring ownership, prefer the most restrictive option.
about collections I can't understand.
a) What do they mean about most restrictive collection? I suppose that they imply inheritance of interfaces that goes as follows:
IReadOnlyList<T>
inheritsIReadOnlyCollection<T>
which inheritsIEnumerable<T>
. SoIEnumerable
is the most restrictive one?b) And most importantly why does it matter? At any time anyone can cast above-mentioned collections to
List<T>
and change contents - I see no reason to choose "most restrictive option"Do they require to return only interfaces? Does it mean that I must always cast my
List<T>
toIEnumerable<T>
- isn't it misleading or hitting performance?They mention transferring ownership. When do I want to tranfer ownership and when I do not want to do that? And if I do why