I don't have any idea about when it's needed to use a list instead of ICollection or IEnumerable (and, of course, arrays). I personally use collection when I need to iterate and modify the collection and enumerables when I just need to iterate over it.
-
Can you point to articles you have read on the issue? – mjwills Sep 11 '19 at 12:25
-
Don't forget `IReadOnlyList
` and `IReadOnlyCollection – Matthew Watson Sep 11 '19 at 12:26` too! But this is a question for another site, really. -
Well, a rule of thumb is - for *input* parameter the more *general* the better, i.e. `void MyMethod(IEnumerable
value)` is better than `void MyMethod(List – Dmitry Bychenko Sep 11 '19 at 12:29value)`; quite the opposite for *return* values: `List MyMethod()` is now better then `IEnumerable MyMethod()` -
See this link for example: https://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work/3628705 – MakePeaceGreatAgain Sep 11 '19 at 12:30
1 Answers
- When and why we should choose IEnumerable
This is the base interface for any collection under system.Collection. If you want iteration only, then it is the best choice to use. Iteration means that you do not want any index based search or any write or any insert or remove.(only read records). You can only read records, nothing else.
public interface IEnumerable {
// Summary:
// Returns an enumerator that iterates through a collection.
//
// Returns:
// An System.Collections.IEnumerator object that can be used to iterate through
// the collection.
[DispId(-4)]
IEnumerator GetEnumerator();
}
IEnumerable
contains only GetEnumerator()
method, like read-only iterate.
- When and why we should use ICollection
ICollection is one step ahead of IEnumerable. If we want some more functionality like Add or remove element, then it is better to go with ICollection because we cannot achieve that with IEnumerable. ICollection extends IEnumerable. It supports non-index based operations like - Add item in Collection, remove, check contained item etc. ICollection has "Add" method (see in screenshot 2). So, there is not any index as a parameter to insert or remove element at a particular index.
[ComVisible(true)]
public interface ICollection: IEnumerable {
int Count {
get;
}
bool IsSynchronized {
get;
}
object SyncRoot {
get;
}
void CopyTo(Array array, int index);
}
- When and why we use IList
This is the only interface in the System.Collection that contains all functionality of IEnumerable and ICollection and additional functionality.
As you are seeing in the below code, IList has Insert and Remove methods. Both the methods accept index in their parameter. So, it supports index based operations over collection.
IList is the last interface in the hierarchy of all collection interfaces. It extends ICollection.
[ComVisible(true)]
public interface IList: ICollection, IEnumerable {
bool IsFixedSize {
get;
}
bool IsReadOnly {
get;
}
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);
}
IList can accept any kind of Collection Example we can pass
List < string > lst = new List < string > ();
IList < string > str = lst;
OR
string[] arr = new string[] {
"one",
"two",
"three"
};
IList < string > str = arr;
OR
ArrayList lst = new ArrayList();
lst.Add("tanuj");
IList str = lst;
- When and why we use IList , why IList and Not List
List is the concrete class. This class implements IList, ICollection, IEnumerable. IList provides us full flexibility to expose any method that list implements.
If we are making a class library and providing the DLL to two different clients to expose functionality, it's easy to make any change in interface rather than class. Below is the example .
If we expose Class OR LIST outside the world, it is more difficult to handle every client that is using the same functionality. So, never change in concrete class. Always expose interface.

- 10,636
- 1
- 28
- 46