using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
public class MyEnumerable : IEnumerable
{
public MyEnumerable( T[] items ) {
this.items = items;
}
public IEnumerator GetEnumerator() {
return new NestedEnumerator( this );
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
// The enumerator definition.
class NestedEnumerator : IEnumerator
{
public NestedEnumerator( MyEnumerable coll ) {
Monitor.Enter( coll.items.SyncRoot );
this.index = -1;
this.coll = coll;
}
public T Current {
get { return current; }
}
object IEnumerator.Current {
get { return Current; }
}
public bool MoveNext() {
if( ++index >= coll.items.Length ) {
return false;
} else {
current = coll.items[index];
return true;
}
}
public void Reset() {
current = default(T);
index = 0;
}
public void Dispose() {
try {
current = default(T);
index = coll.items.Length;
}
finally {
Monitor.Exit( coll.items.SyncRoot );
}
}
private MyEnumerable coll;
private T current;
private int index;
}
private T[] items;
}
public class EntryPoint
{
static void Main() {
MyEnumerable integers = new MyEnumerable( new int[] {1, 2, 3, 4} );
foreach( int n in integers ) {
Console.WriteLine( n );
}
}
}