Chciałbym, aby mój typ implementował IEnumerable<string>
. Starałem się śledzić C# w pigułce, ale coś poszło nie tak:Jak zaimplementować IEnumerable <T> za pomocą narzędzia GetEnumerator()?
public class Simulation : IEnumerable<string>
{
private IEnumerable<string> Events()
{
yield return "a";
yield return "b";
}
public IEnumerator<string> GetEnumerator()
{
return Events().GetEnumerator();
}
}
Ale pojawia się błąd kompilacji
Error 1 'EventSimulator.Simulation' nie wykona „System.Collections członków interfejs. IEnumerable.GetEnumerator() ". "EventSimulator.Simulation.GetEnumerator()" nie może zaimplementować "System.Collections.IEnumerable.GetEnumerator()", ponieważ nie ma zgodnego typu powrotu "System.Collections.IEnumerator".
Na marginesie: możesz "zwrócić return' bezpośrednio od wewnątrz' GetEnumerator() ' – AlexFoxGill