In this article, I will discuss the type of iterators that are supported by C# and VB.NET.

.NET Framework usually supports two different kinds of the 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 an array, dictionary, hash table, and the customized collection. But this consist of several moving parts. Few developers may even argue that it is quite an 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 a similar kind of output, but above is quite better than the old one as the former one consisting of the moving parts. Though, quite more vital thing is that there are some semantical differences between every construct, so we should be 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 print the initial value from a collection as variable ‘i’ is mutable (reinitialize the value) and this 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 the occupation variable. When we try this, the 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 is a part of such type of programming style which is called an imperative programming style.

Internal iterator:

Here, the expression is quite brief and expressive. Now let us have a look atentirely the 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 function 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 an external iterator (for and for each). Though the code snippet is also quite declarative.

Here, the word declarative means that we 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 portion 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 making great manual loops with the data structure, you need to wait for just a few minutes and then start thinking for internal iterator. The reason is that power of language C# is great, but most significant 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 frameset of the functions like.

Related Articles:

Last modified: March 8, 2019

Comments

Write a Reply or Comment

Your email address will not be published.