Tech Off Post

Single Post Permalink

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

    TommyCarlier said:
    TommyCarlier said:

    Actually, now that I think about it further: it could be a good idea to move all the members of ICollection<T> and IList<T> that can be performed on read-only collections to interfaces IReadOnlyCollection<T> and IReadOnlyList<T>. It would allow for richer API's without additional overhead. The current collection classes would not have to be changed. The only thing that changes is the addition of these interfaces and the modification of ICollection<T> and IList<T> (so they implement IReadOnlyCollection<T> and IReadOnlyList<T>).

    The interface hierarchy would look like this:
     public interface IEnumerable<T> : IEnumerable {   IEnumerator<T> GetEnumerator(); } public interface IReadOnlyCollection<T> : IEnumerable<T> {   int Count { get; }   bool Contains(T item);
       void CopyTo(T[] array, int arrayIndex); } public interface IReadOnlyList<T> : IReadOnlyCollection<T> {   T this[int index] { get; }   int IndexOf(T item); } public interface ICollection<T> : IReadOnlyCollection<T> {   void Add(T item);   void Clear();   bool
     Remove(T item);   bool IsReadOnly { get; } } public interface IList<T> : ICollection<T>, IReadOnlyList<T> {   void Insert(int index, T item);   void RemoveAt(int index);   T this[int index] { get; set; } }
    I've extended the idea to read-only dictionaries (IReadOnlyDictionary<TKey, TValue>) and just wrote a blog post about it: .NET BCL proposition: read-only collection interfaces. Because I think this idea is solid and would be a good addition to the BCL, I sent an e-mail to the .NET BCL Team. Let's see if they respond.