The interface that you implement in turn is implemented by the non-typed IEnumerable:
public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); } public interface IEnumerator<out T> : IDisposable, IEnumerator { T Current { get; } } public interface IEnumerator { object Current { get; } bool MoveNext(); void Reset(); }
Therefore, you need to implement its "insides", namely, another GetEnumerator () method and IEnumerator <out T>:
using System.Collections; using System.Collections.Generic; public class GarageEnumerator<T> : IEnumerator<T> { private readonly IEnumerator enumerator; public GarageEnumerator(IEnumerator enumerator) { this.enumerator = enumerator; } public void Dispose() { } public bool MoveNext() { return enumerator.MoveNext(); } public void Reset() { enumerator.Reset(); } public T Current { get { return (T)enumerator.Current; } } object IEnumerator.Current { get { return Current; } } } public class Garage<T> : IEnumerable<T> { private readonly T[] carList; //initialization ! public IEnumerator<T> GetEnumerator() { return new GarageEnumerator<T>(carList.GetEnumerator()); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
In general, use the built-in tools in VS - it can do it for you.
UPDATE: from 3.5 framework you can connect:
using System.Linq;
and implementation:
public class Garage<T> : IEnumerable<T> { private readonly T[] carList = new T[10]; //initialization ! public IEnumerator<T> GetEnumerator() { return carList.Cast<T>().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
IEnumerator
onSystem.Collections.IEnumerator
, or add the appropriateusing
.Inumerator
andIEnumerator<T>
are different types, they are in different namespaces, but the second interface requires the implementation of the first. - AlexeyM