What are iterators?

Answers were Sorted based on User's Feedback



What are iterators?..

Answer / sumit ranjan

Iterators is an object that allows a programmer to traverse
through all the elements of a collection, regardless of its
specific implementation.

Is This Answer Correct ?    6 Yes 0 No

What are iterators?..

Answer / gp_bellamkonda

What Sumit suggested was correct.Adding few more points to
it.

--The iterator code uses the yield return statement to
return each element in turn. yield break ends the
iteration. For more information, see yield.

--Multiple iterators can be implemented on a class. Each
iterator must have a unique name just like any class
member, and can be invoked by client code in a foreach
statement as follows: foreach(int x in
SampleClass.Iterator2){}

--The return type of an iterator must be IEnumerable,
IEnumerator, IEnumerable<T>, or IEnumerator<T>.

Is This Answer Correct ?    0 Yes 0 No

What are iterators?..

Answer / srinivas.r

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. For
more information, see yield.

Multiple iterators can be implemented on a class. Each
iterator must have a unique name just like any class
member, and can be invoked by client code in a foreach
statement as follows: foreach(int x in
SampleClass.Iterator2){}

The return type of an iterator must be IEnumerable,
IEnumerator, IEnumerable<T>, or IEnumerator<T>.

Is This Answer Correct ?    0 Yes 0 No

What are iterators?..

Answer / 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

What are iterators?..

Answer / venkat2050

iterators in dot net is we can write one class inside
another class we cannot create an object from another class

Is This Answer Correct ?    0 Yes 6 No

Post New Answer

More C Sharp Interview Questions

What is the main purpose of delegates in c#?

0 Answers  


What is early binding and late binding

12 Answers   ABC, Infosys, TATA,


What is “using” statement in c#?

0 Answers  


Explain the features of an abstract class in net.

0 Answers  


Explain briefly the difference between value type and reference type?

0 Answers   Accenture,






Is nullable type c#?

0 Answers  


What is the resgen.exe tool used for?

0 Answers  


Is and as in c#?

0 Answers  


how to implement a web service in .net

0 Answers  


What's difference about switch statements in C# ?

1 Answers  


What is the purpose of dictionary in c#?

0 Answers  


how can include .netframeworl 2.0 in application setup

1 Answers  


Categories