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 a for-each loop.
So if your 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. 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#, 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 that for-each does, like disposing of 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 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 that allows list types to be used 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.
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);
}
}
}
Leave Comment