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
Is c# a technology?
Why static constructor is parameterless in c#?
What is the xml document structure?
Can a child class call the constructor of a base class?
What is the difference between list and array in c#?
How to use delegates with events?
What is jit (just in time)?
what will be the output of the given below coding. using System; public class Exercise { static void OddNumbers(int a) { if (a >= 1) { Console.Write("{0}, ", a); a -= 2; OddNumbers(a); } } public static int Main() { const int Number = 9; Console.WriteLine("Odd Numbers"); OddNumbers(Number); Console.WriteLine(); return 0; } }
How to override a function in c#?
What is the main method?
What is the object class in c#?
What is generic delegate in c#?
Contrast System.String and System.Text.StringBuilder classes?
What is the base class in .net from which all the classes are derived from?
Define an array?