Collection in C# - Part One

C# provides a set of collections. These include Lists, LinkLists, Array, ArrayList, Dictionary and many more. We will discuss all the collection one-by-one in this series.

Enumeration

In C# there are many types of collections, from simple to complex. These collections differ from each other but traversing is a universal need. Traversing is supported by IEnumerable, IEnumerator, and their generic part IEnumerable<T>, IEnumerator<T>. IEnumerable, IEnumerator belongs to System.Collections namespace and IEnumerable<T>, IEnumerator<T>
belongs to System.Collections.Generic namespace.

IEnumerator

IEnumerator interface defines a basic low level protocol by which a collection is traversed in forward-only approach. If you see implementation in visual studio is looks like the following,

  1. public interface IEnumerator  
  2. {  
  3.     object Current  
  4.     {  
  5.         get;  
  6.     }  
  7.     bool MoveNext();  
  8.     void Reset();  
  9. }  
Inthe above definition current gives the current element. MoveNext moves the cursor from current element to next element and if there is no new element it returns false. Reset method moves cursor back to start and the collections are enumerated again.

Example 1:
  1. static void Main(string[] args)  
  2. {  
  3.     int[] number = new int[]   
  4.     {  
  5.         1,  
  6.         2,  
  7.         3,  
  8.         4  
  9.     };  
  10.     IEnumerator enumerator = number.GetEnumerator();  
  11.     while (enumerator.MoveNext())   
  12.     {  
  13.         Console.WriteLine("Number is {0}", enumerator.Current);  
  14.     }  
  15. }  
Output:

Number is 1
Number is 2
Number is 3
Number is 4

Example 2:
  1. class Program  
  2. {  
  3.     static void Main(string[] args)   
  4.   {  
  5.         string software = "SOFTWARE";  
  6.         IEnumerator enumerator = software.GetEnumerator();  
  7.         while (enumerator.MoveNext())  
  8.         {  
  9.             Console.WriteLine(enumerator.Current);  
  10.         }  
  11.   
  12.     }  
  13. }  
Output

S
O
F
T
W
A
R
E

IEnumerable

Collection doesn’t implement IEnumerator directly. IEnumerator is implemented by IEnumerable. If you check in visual studio all the collection is derived from IEnumerable and others. IEnumerable is the most basic interface that collection classes implement.

Implementation of IEnumerable
  1. namespace System.Collections  
  2. {  
  3.     public interface IEnumerable  
  4.     {  
  5.         IEnumerator GetEnumerator();  
  6.     }  
  7. }
Example
  1. static void Main(string[] args)  
  2. {  
  3.     string software = "SOFTWARE";  
  4.     IEnumerable enumerable = software;  
  5.     foreach(var item in enumerable)  
  6.     {  
  7.         Console.WriteLine(item);  
  8.     }  
Output

S
O
F
T
W
A
R
E

Now you are thinking we can traverse string without converting to IEnumerable. Yes we can traverse because string also implements IEnumerable. Above is just an example to show you.

IEnumerator<T>

IEnumerator<T> is a generic part of IEnumerator. IEnumerator<T> is in System.Collections.Generic namespace and it implements IDisposable, IEnumerator interface.

IEnumerator inherits from IDisposable that means resources are released when enumeration is completed.

Implementation of IEnumerator
  1. namespace System.Collections.Generic  
  2. {  
  3.     public interface IEnumerator < out T >: IDisposable, IEnumerator  
  4.     {  
  5.         T Current  
  6.         {  
  7.             get;  
  8.         }  
  9.     }  
  10. }  
IEnumerable<T>

IEnumerable <T> is a generic part of IEnumerable. IEnumerable <T> is in System.Collections.Generic namespace and it implements IEnumerable interface.

Implementation of IEnumerable<T>
  1. namespace System.Collections.Generic  
  2. {  
  3.     public interface IEnumerable < out T >: IEnumerable  
  4.     {  
  5.         IEnumerator < T > GetEnumerator();  
  6.     }  
  7. }  
Generic strengthens type safety and reduces the overhead of boxing.