C# Programming > Data

C# Iterators

C# Iteration

C# Iteration is scanning a collection (or an array), viewing each element one at a time. An iterator can have additional rules as to how elements are visited, but for this article we will stick with scanning C# collections one element at a time.

There are three major methods to iterate in C#.NET: for loop, foreach loop, and enumerators.

For Loop

A for loop iterates through a C# collection by accessing elements through indices. The loop basically increases an integer value which is then used to request an object from the collection. This means that a collection must support access by indices to support a for-loop iteration.

An example of a for loop is:

for (int i = 0; i < collection.Count; i++)
{
   object o = collection[i];
}

A quick test showed that a for-loop took 762 milliseconds to iterate through a collection of 100,000,000 integer elements.

Advantages of a for-loop include the flexibility of the iteration. The line i++ along with commands such as break and continue make controling iterations with for-loops very simple. Index access of objects also allows objects inside the collection to be directly modified or replaced.

Disadvantages include the slight lack of readability. Using brackets [] to access objects can get confusing when used extensively.

Foreach Loop

A C# foreach loop iterates though a C# collection almost automatically. The programmer only needs to specify the type of objects to find inside a collection and C#.NET will automatically iterate through all the possible elements.

An example of a foreach loop is:

foreach (int i in collection)
{
   //i is current element
}

The same test showed that a foreach-loop took 983 ms to iterate through the entire collection. Why is foreach slower? One of the reasons is that it has to allocate a new variable (in this case i) for every element of the C# collection.

Advantages are obviously the simplicity of the resulting C# code. Not only is it easier to write a foreach loop but working with elements is cleaner since each element is assigned to a new variable.

Disadvantages are the occasional performance loss. Also it is no longer simple to modify the element being viewed because it was internally assigned to another variable. It also makes it hard to replace elements in a collection.

Enumerator

A C# enumerator (not to be confused with a C# enum) is the most abstract form of an iterator. Any C# Collection that inherits the ICollection interface (which includes most of the collections) have an enumerator.

An example modeled after the previous iterators:

IEnumerator<int> enumerator = k.GetEnumerator();
bool hasNext = enumerator.MoveNext();
while (hasNext)
{
    int i = enumerator.Current;
    hasNext = enumerator.MoveNext();
}
enumerator.Dispose();

The same test once again was ran and showed that an enumerator took 1202 ms to iterate through the collection.

Advantages are many. Notice how the variable is an interface (IEnumerator). This makes the code very abstract and easily adjustable to future changes in the code. Although in this example the iterator is a generic one (for integer) there is also a non-generic IEnumerator that works with objects.

Disadvantages are somewhat slower code and code that is less clean and straight forward as a for-loop or foreach-loop iteration.

Back to C# Article List