public class Stack<T> : IEnumerable<T>
{
public void Push(T item)
{
if (items == null)
{
items = new T[4];
}
else if (items.Length == count)
{
T[] newItems = new T[count * 2];
Array.Copy(items, 0, newItems, 0, count);
items = newItems;
}
items[count++] = item;
}
public T Pop()
{
T result = items[--count];
//items[count] =T.default;
return result;
}
public IEnumerator<T> GetEnumerator()
{
for(int i = count - 1; i >= 0; --i)
{
yield return items[i];
}
}
//public Stack()
//{
// items = new T[10];
// count = 0;
//}
public T[] items;
public int count=0;
}
提示错误:
Error 1 'WindowsApplication2.Stack<T>' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'WindowsApplication2.Stack<T>.GetEnumerator()' is either static, not public, or has the wrong return type. E:\WebSites\WindowsApplication2\WindowsApplication2\Form1.cs 126 18 WindowsApplication2