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 typeof undefined?
Is lazy thread safe c#?
What is difference between dictionary and list in c#?
Is string nullable c#?
What do you mean by winforms in c#?
What is a virtual property. Give an example?
Explain the difference between abstract class and interface.
Is dll a library?
Are arrays value types or reference types?
Can main method be final?
What is arraylist class in c#?
What is regex c#?
Why is static constructor called first?
Is c# scripting language?
There were a lot of questions asked, so I will list the topic (and add a what is "topic" and know pros/cons). Extreme programming, what is a transaction, various SDLC design approaches, what is a namespace, define a good test case, what is a stored proc, webservice? design patterns? linker? compiler? access modifiers? stack vs. queue? arrays vs. linked lists? sorting algorithms? recursion? OOP principles?