2

Is there a particular reason to why the generic counterparts of IList and ICollection do not have the same set of methods and properties? They seem to have moved them around.

Ex. IList<T> has

int IndexOf(T item);
void Insert(int index, T item);
void RemoveAt(int index);
T this[int index] { get; set; }

But IList has

int Add(object value);
void Clear();
bool Contains(object value);
int IndexOf(object value);
void Insert(int index, object value);
void Remove(object value);
void RemoveAt(int index);
bool IsFixedSize { get; }
bool IsReadOnly { get; }
object this[int index] { get; set; }
Magnus
  • 45,362
  • 8
  • 80
  • 118

2 Answers2

0

IList<T> implements ICollection<T>, so at least in the context of an IList<T> it doesn't matter which interface has what. As far as why, who knows what goes on in the minds of that crazy .NET team. However, it seems logical that a "collection" would need the methods to add, remove, check its contents, etc AS LONG AS those methods are not in the context of an indexed "list". Once you start talking about a list conceptually, you add indexing, which requires the indexer itself, and index-sensitive methods to add, remove and check for elements.

KeithS
  • 70,210
  • 21
  • 112
  • 164
  • 1
    I totally agree with you that it makes sense that ICollection should have all those methods. Strange though that only the generic version has them. – Magnus May 12 '11 at 19:25
  • @Magnus: The `ICollection` includes functionality which should have appeared in a type between the non-generic `ICollection` and `IList`. Conversely, between `ICollection` and `IEnumerable` there should have been a type somewhat like the non-generic `ICollection`, but without the ability to add items. Using the same name `ICollection` when the generic and non-generic things are so different is confusing; the best way to ease that confusion is to ignore the matching names. – supercat Feb 11 '14 at 07:25
0

I think the major design decision came down to the fact that arrays in .Net actually all implement IList<T> implicitly. Many of the methods from IList such as Remove and Clear don't apply to arrays so they were moved to a different interface.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274