This is an extension of Why doesn't generic ICollection implement IReadOnlyCollection in .NET 4.5?
Design and compatibility are the main reasons.
When a method needs to get both ICollection and IReadCollection as parameter, the easiest solution is then to use IEnumerable instead since both extend it.
Then, one could use the Linq Count() method instead of using directly the Count property.
This is an extract of System.Linq.Enumerable.cs from https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,41ef9e39e54d0d0b
public static int Count<TSource>(this IEnumerable<TSource> source) {
if (source == null) throw Error.ArgumentNull("source");
ICollection<TSource> collectionoft = source as ICollection<TSource>;
if (collectionoft != null) return collectionoft.Count;
ICollection collection = source as ICollection;
if (collection != null) return collection.Count;
int count = 0;
using (IEnumerator<TSource> e = source.GetEnumerator()) {
checked {
while (e.MoveNext()) count++;
}
}
return count;
}
It first checks if it is a collection and in that case directly return Count so the performance cost will be minimal. However, why does this this implementation of Count() is does not have a similar check with IReadOnlyCollection?
I don't see compatibility to be an issue here: older code written with .NET<4 that get compiled with .NET 4.5+ can check the IReadOnlyCollection even if nothing use it...