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 c# windows form application?
What is difference between yielding and sleeping?
What are the delegates in c#?
What is dll file in c#?
Explain Constructor and destructor?
If c# destructors are so different to c++ destructors, why did ms use the same syntax?
What is the property of a class in c#?
Define a partial class?
How do I create a multilanguage, single-file assembly?
What does int32 mean in c#?
Define assert() method? How does it work?
Is it true that all c# types derive from a common base class?
Why do we use classes?
What are the fundamental differences between value types and reference types?
What are All kind of access specifiers for a class and for methods