---
title: "IEnumerable, IEnumerator, ICollection and IList in C#"  
description: "IEnumerable, IEnumerator, ICollection and IList in C#"  
author: "AVADHESH PATEL"  
published: 2012-12-27  
updated: 2019-11-26  
canonical: https://www.mindstick.com/articles/1091/ienumerable-ienumerator-icollection-and-ilist-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# IEnumerable, IEnumerator, ICollection and IList in C#

### What is IEnumerable, IEnumerator, ICollection and IList in C#

##### \

#### IEnumerable

IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface; this, in turn, allows read-only access to a collection.

The collection that implements IEnumerable can be used with a foreach statement.

IEnumerable enables you to iterate [through the collection using](https://www.mindstick.com/Articles/301/a-label-control-in-vb-dot-net) a for-each loop.

So [if your](https://yourviews.mindstick.com/view/83528/what-to-do-if-your-eye-glasses-don-t-fit) intention is just that, IEnumerable can help you achieve it with minimum effort ([implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) of only one method - GetEnumerator()).

List, on the other hand, is a pre-implemented type-safe collection (using generic type) class available in the framework. This already has the implementation of IList, ICollection & IEnumerable.

So functionally IEnumerable is a subset of List. Also, List is a class whereas IEnumerable would need implementation.

#### IEnumerator

IEnumerator allows you to iterate over a list, array, etc. (anything that implements IEnumerable) and process each element one-by-one.

If you're familiar with a for-each [loop in C#](https://www.mindstick.com/forum/33669/while-loop-in-c-sharp), IEnumerator is what it uses under the covers. For

##### Example

```
List<string> Items = new List<string>() {  "pen", "book", "bag"  }; 
foreach(string  itm  in Items){
    Console.WriteLine(itm  );
}
```

Is actually translated to something like this:

```
List<string>Items =newList<string>(){"pen", "book", "bag" }; 
IEnumerator<string> enumerator =list.GetEnumerator(); 
while(enumerator.MoveNext()){
stringitm= enumerator.Current; Console.WriteLine(itm);
}
```

This is simplified, as there are other cleanup [operations](https://www.mindstick.com/news/2675/bmw-agrees-to-integrate-blockchain-with-operations-partners-with-bnb-chain-coinweb) that for-each does, like disposing of the enumerator if it implements IDisposable, but this addresses the relevant portion.

In fact, [your entire](https://yourviews.mindstick.com/view/87869/how-can-reading-a-book-change-your-entire-life) try-finally code block is almost a dead-on copy of how the for-each construct gets translated into IL.

#### ICollection

ICollection is an interface, you can't instantiate it directly. You'll need to instantiate a class that implements ICollection; for example, List<T>. Also, the ICollection interface doesn't have an Add method -- you'll need something that implements IList or IList<T> for that.

For example:

```
List<object> icollection =newList<object>();
icollection.Add("your item here");
```

#### IList

Lists and arrays implement IList. This interface is an [abstraction](https://www.mindstick.com/articles/944/abstraction-in-c-sharp) that allows list types to be used through a single [reference type](https://www.mindstick.com/interview/235/what-is-difference-between-value-types-and-reference-types-in-vb-dot-net-or-c-sharp-value-types-vs-reference-types). With it, we can create a single method to receive an int[] or a List<int>.

First, with the IList [generic interface](https://www.mindstick.com/forum/160400/how-to-implement-a-generic-interface-in-c-sharp-provide-an-example), you must specify a type parameter. If you want your method to act upon ints, you can use IList<int>. Any type (string, object) can be specified.

Note: In this program, we introduce the Display method, which receives an IList<int> parameter.

##### Example:

```
using System;using System.Collections.Generic;
class Program{static void Main(){
int[] array = new int[3];array[0] = 1;array[1] = 2;array[2] = 3;Display(array);
List<int> list = new List<int>();list.Add(5);list.Add(7);list.Add(9);Display(list);
}
static void Display(IList<int> list){Console.WriteLine("Count: {0}", list.Count);foreach (int value in list){Console.WriteLine(value);}
}
}
```

---

Original Source: https://www.mindstick.com/articles/1091/ienumerable-ienumerator-icollection-and-ilist-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
