Home > DeveloperSection > Articles > IEnumerable, IEnumerator, ICollection and IList in C#

IEnumerable, IEnumerator, ICollection and IList in C#

C# C# 
0 Comment(s)
 11471  View(s)
Rate this:

IEnumerable, IEnumerator, ICollection and IList in C#


IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface; this in turn allows readonly access to a collection. A collection that implements IEnumerable can be used with a foreach statement.

IEnumerable enables you to iterate through the collection using a for-each loop.

So if you intention is just that, IEnumerable can help you achieve it with minimum effort (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. It already has the implementation of IList, ICollection & IEnumerable.

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



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 foreach loop in C#, IEnumerator is what it uses under the covers. For



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();
stringitm= enumerator.Current;

This is simplified, as there are other cleanup operations that foreach does, like disposing the enumerator if it implements IDisposable, but this addresses the relevant portion.

In fact, your entire try-finally code block is almost a dead-on copy of how the foreach construct gets translated into IL.



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");


Lists and arrays implement IList. This interface is an abstraction that allows list types to be used with through a single reference type. With it, we can create a single method to receive an int[] or a List<int>.

First, with the IList generic interface, 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.



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;
List<int> list = new List<int>();
static void Display(IList<int> list)
Console.WriteLine("Count: {0}", list.Count);
foreach (int value in list)

Don't want to miss updates? Please click the below button!

Follow MindStick