Generally, developers are confused about when and why should they use IEnumerable OR ICollection OR IList Or List. So, in this article, I am exploring more when and why to use each.
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 {
-
-
-
-
-
-
- [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.
Exposing Interface Example
In the below example, we are exposing Interface.
- public interface I1 {
- void Display();
- }
- public class A: I1 {
- public void Display() {
- Console.WriteLine("Dislay Calling");
- }
- }
- static void Main(string[] args) {
- I1 interfaceExpose = new A();
- interfaceExpose.Display();
- }
So, after some time, one client says I need to add Display2() functionality. Then, we need to add Display2() function in interface and access that method easily.
Here is the Problem with Exposing Class.
- public interface I1 {
- void Display();
- }
- public class A: I1 {
- Public void Display() {
- Console.WriteLine("Dislay Calling");
- }
- Public void Displat2()
- {
-
- }
- }
- static void Main(string[] args) {
- I1 interfaceExpose = new A();
- interfaceExpose.Display();
- interfaceExpose.Display2();
-
- }
It gives error that It cannot access Display2 () function.
Conclusion
Move to expose interface. Never do any changes in concrete class. Leave concrete class for implementation.