Here's a simple code sample that shows how to build your own enumerable collection
public class MyClass
{
}
public class MyClassCollection : IEnumerator, IEnumerator<MyClass>, IEnumerable, IEnumerable<MyClass>
{
private int index;
private List<MyClass> collection;
public MyClassCollection()
{
collection = new List<MyClass>();
}
public MyClassCollection(IEnumerable<MyClass> items)
{
index = -1;
collection = new List<MyClass>(items);
}
public MyClass this[int index]
{
get { return collection[index]; }
set { collection[index] = value; }
}
public void Add(MyClass item)
{
collection.Add(item);
}
MyClass IEnumerator<MyClass>.Current
{
get
{
if (index == -1)
return null;
else
return collection[index];
}
}
void IDisposable.Dispose()
{
collection = null;
}
object IEnumerator.Current
{
get
{
if (index == -1)
return null;
else
return collection[index];
}
}
bool IEnumerator.MoveNext()
{
index++;
return index >= collection.Count;
}
void IEnumerator.Reset()
{
index = -1;
}
IEnumerator<MyClass> IEnumerable<MyClass>.GetEnumerator()
{
return (collection as IEnumerable<MyClass>).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return (collection as IEnumerable).GetEnumerator();
}
}