我必須創建自己的循環列表,我使用通用列表。如何創建我的循環列表的GetEnumerator?
第一i-創建Node<D>
類代表的數據,並在下一個元件
private class Node<D> {
public D info;
public Node<D> next;
public Node() {
}
public Node(D p) {
info = p;
}
}
要創建循環列表,我創建circularList<T>
類。此類使用Node<>
作爲元素的項目。
這裏是CircularList<T>
類
class CircularList<T> : IEnumerable<T> {
public Node<T> start;
public Node<T> rear;
public int count = 0;
public CircularList(T firstItem) {
start = new Node<T>(firstItem);
rear = start;
rear.next = start;
}
public void Insert(T newItem) {
//Inserting code here
}
public void Update(T oldItem, T newItem) {
//Updating code is here
}
public void Delete(T theItem) {
//deleting code is here
}
}
當我使用foreach
foreach(string item in CircularList<string>){
}
我得到了一個錯誤說,circularlist
類需要GetEnumerator()
開始循環。
其實我可以循環所有我的通知列表,但我使用do-while
,我需要Node<T>
來啓動循環。但我不想用Node
和do-while
。
如何創建GetEnumerator()?
任何幫助表示讚賞。 :)
謝謝
注意:我真的,真的不理解IEnumerable
和那些事,請到容易的例子和說明。
你實現'IEnumerable'的'GetEnumerator()'? foreach需要'GetEnumerator()'。如果你還沒有實現,你應該會得到'編譯時間錯誤'。 – ANewGuyInTown
是的,但我不知道該返回什麼。 –