Difference Between IEnumerable, ICollection And IList Interface

IEnumerable, ICollection and IList are interfaces in the .Net Framework used the most often. IEnumerable is the base of the ICollection and IList interfaces (and many other). All these interfaces provide various functionalities and are useful in various cases.

IEnumerable Interface

IEnumerable interface is used when we want to iterate among our classes using a foreach loop. The IEnumerable interface has one method, GetEnumerator, that returns an IEnumerator interface that helps us to iterate among the class using the foreach loop. The IEnumerator interface implements two the methods MoveNext() and Reset() and it also has one property called Current that returns the current element in the list.

I have created a class StoreData for holding an integer type of data and this class implements the IEnumerable interface. Internally I have used a linked list for holding the data (you can find the advantages of a linked list from my previous article “http://www.c-sharpcorner.com/UploadFile/78607b/overview-of-linked-list/).

  1. class StoreData : IEnumerable  
  2. {  
  3.     LinkedList<int> items = new LinkedList<int>();   
  4.     public void Add(int i)  
  5.     {  
  6.        items.AddLast(i);  
  7.     }  
  8.     public IEnumerator GetEnumerator()  
  9.     {  
  10.        foreach (var item in items)  
  11.        {  
  12.           yield return item;  
  13.        }  
  14.     }  
  15. }  
In the preceding code I have created a custom storage list in which I can store integers (practically we can store any type of data depending on our requirements). We can use the preceding list as:
  1. static void Main(string[] args)  
  2. {  
  3.     StoreData list = new StoreData();  
  4.     list.Add(1);  
  5.     list.Add(2);  
  6.     list.Add(3);  
  7.     list.Add(4);  
  8.   
  9.     foreach (var item in list)  
  10.     {  
  11.         Console.WriteLine(item);  
  12.     }  
  13.               
  14.     Console.ReadLine();  
  15. }  
The preceding code will display all the values using a foreach loop. Instead of a foreach loop we can also use the following code:
  1. IEnumerator enumerator = list.GetEnumerator();  
  2.   
  3. while (enumerator.MoveNext())  
  4. {  
  5.    Console.WriteLine(enumerator.Current);  
  6. }  
Behind the scenes the foreach loop works as in the preceding code. The GetEnumerator() method is available with a list object since it implements the IEnumerable interface. Then by using the MoveNext() method and the Current property of the StoreData class we can display the data.

Note

Every collection inside the .Net Framework implements the IEnumerable interface.

Key points of IEnumerable Interface

It provides read-only access to collections. We cannot change any item inside an IEnumerable List. It provides a sort of encapsulation in cases where we don't want our list to be changed.

If we are dealing with some SQL queries dynamically then it also provides lazy evaluation. That means the queries will not be executed until we explicitly need them.

ICollection Interface

The ICollection interface is inherited from the IEnumerable interface which means that any class that implements the ICollection interface can also be enumerated using a foreach loop. In the IEnumerable interface we don't know how many elements there are in the collection whereas the ICollection interface gives us this extra property for getting the count of items in the collection. The ICollection interface contains the following,
  • Count Property
  • IsSynchronized Property
  • SyncRoot Property
  • CopyTo Method

The Count property is used for maintaining the count of elements in the list whereas the IsSysnchronized and SyncRoot properties help to make the collection thread-safe. The CopyTo method copies the entire collection into an array.

The generic version of this interface provides Add and Remove methods also.

IList Interface

The IList interface implements both ICollection and IEnumerable interfaces. This interface allows us to add items to and remove items from the collection. It also provides support for accessing the items from the index. This interface has more power than the preceding two interfaces.

The IList interface contains the following,

  1. IsFixedSize Property
  2. IsReadOnly Property
  3. Indexer
  4. Add Method
  5. Clear Method
  6. Contains Method
  7. Indexof Method
  8. Insert Method
  9. Remove Method
  10. RemoveAt Method

The IList interface has one indexer by which we can access any element by its position and can insert an element and remove an element at any position.

Next Recommended Readings