Hi Guys,
I am reading more on design patterns from this article after little theory session here is working code. Key point was all of the collection classes in the System.Collections namespace, as well as arrays, implement IEnumerable and can therefore be iterated over.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class IteratorPattern
{
static void Main(string[] args)
{
int[] values = new int[] { 1, 2, 3, 4, 5 };
IEnumerator<int> e = ((IEnumerable<int>)values).GetEnumerator();
while (e.MoveNext())
{
Console.Write(e.Current.ToString() + " ");
}
Console.ReadKey();
}
}
}
|