I am a VB.Net developer, kind of newbie in C#,
While looking in C# documentation I came through Iterators and Generators, could not fully understand the use, I there anyone who can explain (in vb perceptive; if possible)
I am a VB.Net developer, kind of newbie in C#, While looking in C#
Share
Iterators are an easy way to generate a sequence of items, without having to implement
IEnumerable<T>/IEnumerator<T>yourself. An iterator is a method that returns anIEnumerable<T>that you can enumerate in a foreach loop.Here’s a simple example:
Notice the
yield returnstatements: these statement don’t actually return from the method, they just “push” the next element to whoever is reading the implementation.When the compiler encounters an iterator block, it actually rewrites it to a state machine in a class that implements
IEnumerable<T>andIEnumerator<T>. Eachyield returnstatement in the iterator corresponds to a state in that state machine.See this article by Jon Skeet for more details on iterators.