Answer Posted / prem
An iterator is invoked from client code by using a foreach
statement.
An iterator is a section of code that returns an ordered
sequence of values of the same type.
An iterator can be used as the body of a method, an
operator, or a get accessor.
The iterator code uses the yield return statement to return
each element in turn. yield break ends the iteration
EXAMPLE
.............................................................
public class DaysOfTheWeek : System.Collections.IEnumerable
{
string[] days = { "Sun", "Mon", "Tue", "Wed", "Thr",
"Fri", "Sat" };
public System.Collections.IEnumerator GetEnumerator()
{
for (int i = 0; i < days.Length; i++)
{
yield return days[i];
}
}
}
class TestDaysOfTheWeek
{
static void Main()
{
// Create an instance of the collection class
DaysOfTheWeek week = new DaysOfTheWeek();
// Iterate with foreach
foreach (string day in week)
{
System.Console.Write(day + " ");
}
}
}
// Output: Sun Mon Tue Wed Thr Fri Sat
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What is namespace explain with example?
What is class sortedlist underneath?
What are interfaces in c#?
What is concrete class in c#?
Is c# or c++ better for games?
What is the difference between const and static read-only?
What is event sourcing in c#?
What Is An Interface Class?
What do you mean by default constructor?
What is the difference between field and variable in c#?
Give some examples for built in datatypes in c#?
What is the purpose of dictionary in c#?
How many digits are in an integer?
Are arrays immutable c#?
What is verbatim string literal in c#?