Introduction to iterator in C# and VB.NET

Tutorials PanelPosted by

In this article I will discuss the type of iterators that are supported by C# and look for the elusive differences.

C# usually supports two different kinds of iterator: External and Internal

External iterator:

It is the statement where we should specify every single step for completing the task. You can consider the below code:

C#

List<string> occupations = new List<string> { "Programmer", "Business Analyst", "Tester" , ”Web Designer” };

for (int i = 0; i < occupations.Count; i++)
            {
 
                Console.WriteLine(occupations [i]);
            }

VB.NET

Dim occupations As List(Of String) = New List(Of String)() {"Programmer", "Business Analyst", "Tester", Web, Designer}
Dim i As Integer = 0
Do While (i < occupations.Count)
    Console.WriteLine(occupations(i))
    i = (i + 1)
Loop

It is quite simple. We have already been using such type of codebase in our regular application. We are now iterating over the elements such as string type of the entire collection list. It may also be others such as array, dictionary, hash table and the customized collection. But this consist several moving parts. Few developers may also argue that it is quite old way for iterating over the collection. They can also write the similar codebase in below mentioned ways:

C#

foreach (var occupation in occupations)
           {
               Console.WriteLine(occupation);
            
           }

VB.NET

For Each occupation In occupations
    Console.WriteLine(occupation)
Next

Instead of above, the code produces similar kind of output, but above is quite better than old one as the former one consisting of the moving parts. Though, quite more vital thing is that there are some semantically differences between every construct, so we should be really thoughtful that the only difference is its flexibility!

C#

List<string> occupations = new List<string> { "Programmer", "Business Analyst", "Tester" , ”Web Designer” };


for (int i = 0; i < occupations.Count; i++)
            {
                Console.WriteLine(occupations [i]);
i = 10
            }

VB.NET

Dim occupations As List(Of String) = New List(Of String)() {"Programmer", "Business Analyst", "Tester", Web, Designer}
Dim i As Integer = 0
Do While (i < occupations.Count)
    Console.WriteLine(occupations(i))
    i = 10
    i = (i + 1)
Loop

Above mentioned codebase just print initial value from collection as variable ‘i’ is basically mutable (reinitialize the value) and this clearly affects the iterator state. It is the one which offers big design flow in a loop constructed as ‘i’ must never be mutable while it was iterating index. On the other hand you can look at below mentioned code:

C#

foreach (var occupations in occupations)
            {
                Console.WriteLine(occupation);
               occupation = "Engineer";
            }

VB.NET

For Each occupations in In occupations
    Console.WriteLine(occupation)
    occupation = "Engineer"
Next

Here the variable (occupation) is mainly immutable; this means that we can never change the value of occupation variable. When we try this, compiler will get unhappy and will give above error message of compile-time.
In above mentioned codes (for and for each), we should specify what needs to be done and how it should be done being a part of such type of programming style which is called as imperative programming style.

Internal iterator:

Here, expression is quite brief and expressive. Now let us have a look at below mentioned example:

C#

List<string> occupations = new List<string> { "Programmer", "Business Analyst", "Tester" , ”Web Designer” };
           occupations.ForEach(occupation => Console.Write(occupation));

VB.NET

Dim occupations As List(Of String) = New List(Of String)() {"Programmer", "Business Analyst", "Tester", Web, Designer}
occupations.ForEach(() => {  }, Console.Write(occupation))

Now, for each function is the higher-order of function, this means when the function takes a single or additional functions as the arguments it is known as the high-order function. In above mentioned code snippet which is ForEach function consists of anonymous function which is occupation=>Console where you need to write the occupation.  This created the similar result as external iterator (for and for each). Though the code snippet is also quite declarative in its nature.

Here, the word declarative means that we just need to specify the fact that what we wish to do with every single element, instead of how it is performed.

On the other hand to external iterator where one should specify each minute details such as starting value, the exit condition and others, here we will give the complete control of specific part of the code that underlie the library. Hence, we should focus on significant part of the business logic instead of the thing that we never need to care for it. It even relives us from different duties such as the programmer.

Now let us summarize, while you require doing great manual loops with data structure, you need to wait for just a few minute and then start thinking for internal iterator. The reason is that power of language C# is great but greatest concern is about the mindset. With developer of C#, our mind is completely wired with loop construct of for/foreach as just for long-time since we are much familiar with this. However, not for the reason it offers better way for programming. We need to always re-tune the mindset. The code will also express the ideas in a better and enhanced way. You need to only frame set of the functions like.

Related Articles: