Tech Off Post

Single Post Permalink

View Thread: How about some syntax sugar for IEnumerable<>?
  • User profile image
    dpratt71

    TommyCarlier said:
    One thing I sometimes miss is an interface between IEnumerable<T> and ICollection<T> that represents a read-only collection (implements IEnumerable<T>) with a Count-property. Something like this:
    public interface IReadOnlyCollection<T>
     : IEnumerable<T>
    {
        int Count { get; }
    }
    Of course, thanks to the magic of extension methods, IEnumerable now effectively does have a Count method. I pointed Reflector at the implementation: As you might expect, if the source implements ICollection, then ICollection.Count is returned. Otherwise it enumerates over the collection, incrementing a counter along the way.