foreach enumerator basic c# example



C#, iterators are called enumerators, and they come from the IEnumerator ... Using a foreach loop will allow us to traverse the list.

using System;
using System.Collections;

namespace CSharpCollection
{
    class EnumeratorBasics
    {
		static void Main()
		{
			int[] MyArray = { 10, 11, 12, 13 }; // Create an array.
			
			foreach(int i in MyArray)
				Console.WriteLine("{0}", i); // Write it out.
				
/*			IEnumerator ie = MyArray.GetEnumerator();

			while ( ie.MoveNext() )
			{
				int i = (int) ie.Current;
				Console.WriteLine("{0}", i); // Write it out.
			}
*/
		}
	}
}

0 Comment's

Comment Form