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 does m mean in decimal c#?
What are the different types of classes?
What are custom attributes in c#?
Major difference between Basic httpbinding and WShttpbinding?
Is an array an object c#?
the c# keyword .int. Maps to which .net type?
Is string primitive?
What is lazy loading entity framework?
What is a console?
What is difference between for and foreach loop in c#?
Can destructors have access modifiers?
Do extension methods have to be static?
How do I unload an application domain?
Can we extend sealed class in c#?
What is strongly typed view?