1

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...

Community
  • 1
  • 1
EricBDev
  • 1,279
  • 13
  • 21
  • 1
    I would _guess_ because it's not as generic as `ICollection` which is implemented by all collection types. it wouldn't be reasonable to check every interface that declares a Count property in terms of performance. – Selman Genç Mar 29 '17 at 11:12
  • This is not a problem for the built in collections as they all implement ICollection. – Magnus Mar 29 '17 at 12:47

1 Answers1

0

If you have deal with IReadOnlyCollection you can get count directly from Count property of IReadOnlyCollection. https://msdn.microsoft.com/en-us/library/hh881542(v=vs.110).aspx

It would be wrong to add dependency from general Linq Count() to more specific IReadOnlyCollection.

UPD

Linq Count() it's a core function that works with IEnumerable, so any cast inside it just some performance hack's. Count() extension can't have specific code for each IEnumerable that have count somewhere. If you want you can make extension self. But, probably, naming can be better

gabba
  • 2,815
  • 2
  • 27
  • 48
  • 1
    I didn't downvote, but you did link the Russian version of the MSDN. – krillgar Mar 29 '17 at 11:44
  • @krillgar, thanks, I have fixed that terrible mistake – gabba Mar 29 '17 at 11:55
  • your link refers to the ReadOnlyCollection that implements both ICollection and IReadOnlyCollection interfaces. And that especially because IReadOnlyCollection has a Count property that I was expecting it in Count() method. I was speaking about a method getting IEnumerable parameter that could be IReadOnlyCollection or ICollection. So I still don't see why it would be wrong to add dependency from Linq Count() to IReadOnlyCollection – EricBDev Mar 29 '17 at 19:29
  • @EricBDev, ops. I've change the locale but change link to wrong article. Fixed. thanks to! – gabba Mar 29 '17 at 19:33
  • @EricBDev, Linq Count() it's a core function that works with IEnumerable, so any cast inside it just some performance hack's. Count() extension can't have specific code for each IEnumerable that have count somewhere. If you want you can make extension self. But, probably, naming can be better. – gabba Mar 29 '17 at 19:45
  • ok, the performance cost of checking "as IReadOnlyCollection" which will be false/null in 95% of cases will be indeed a good reason. It was also Selman answer above – EricBDev Mar 29 '17 at 19:53
  • @EricBDev, a performance is good point, but I meant a code organisation principle. Not put to much dependencies to core function - It's good point also. And if we make specific extension for IReadOnlyCollection we kill two birds. – gabba Mar 29 '17 at 21:39