How are iterators used in C# ?

suppose I have say:

List<int> someList = new List<int>();
/* assume some items are added here */

/* and now I wonder if C++ style iterators are available? */

for( var i = /* iterator begin of list */ ; i != /* iterator to end of list */; ++i )
{
   //use iterator.
}

I understand it's common to use the foreach with C# arrays/containers, but I prefer the normal for( ; ; ) { }.

How are iterators used in C# ?

Text from MSDN article :-

An iterator is a method, get accessor, or operator that performs a custom iteration over an array or collection class by using the yield keyword.

I understand it's common to use the foreach with C# arrays/containers, but I prefer the normal for( ; ; ) { }.

Use GetEnumerator() method.

for (IEnumerator<int> iterator = list.GetEnumerator(); iterator.MoveNext(); )
 {
   int value=iterator.Current;
   //
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.