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
Explain the top reason to use c# language?
What is a template class?
Whats an assembly? Describe the importance of assembly?
What is asenumerable in c#?
Define satellite Assembly in .NET?
Difference between directcast and ctype.
Which is faster dictionary or hashtable?
What is string concatenation?
In a site to turn off cookies for one page which method is followed?
Explain deadlock?
Define acid rule of thumb for transactions in c#.
Is string primitive?
What is virtual class in C#?
What is a strong name in c#?
In .NET how can you solve the DLL Hell problem?