Korzystając z kodu podanego na wykładzie chciałem zaimplementować własną kolekcję która posiadałaby pętlę for each. Poniżej zamieściłem kod w zasadzie przepisany z wykładu:
using System.Collections;
class Element<T>
{
public T val;
public Element<T> next;
}
class ListEnum<T> : IEnumerator
{
Element<T> lista;
public ListEnum(Element<T> lista)
{
this.lista = lista;
}
public bool MoveNext()
{
if (this.current == null) this.current = this.lista;
else this.current = this.current.next;
return this.current != null;
}
public object Current
{
get
{
return current.val;
}
}
public void Reset()
{
this.current = this.lista;
}
}
class Lista<T> : IEnumerable
{
Element<T> lista;
public IEnumerator GetEnumerator()
{
return new ListEnum<T>(lista);
}
}
Oczywiście jest to tylko wycinek kodu, ale już tutaj zwraca mi w sumie 7 błedów następującej treści:
Error 1
'ListEnum<T>' does not contain a definition for 'current' and no extension method 'current' accepting a first argument of type 'ListEnum<T>' could be found (are you missing a using directive or an assembly reference?)
No ok, nie mam zdefiniowanego pola current, ale z tego co wyczytałem to jest to właściwość tego interfejsu - dlaczego jej nie widać? Czy ktoś może wie jak to poprawić aby wszystko zadziałało? Ja już szczerze mówiąc mam dość szukania błędów w slajdach wykładowcy, więc byłbym wdzięczny gdyby ktoś umiał pomóc.