下面是例子:using System;
using System.Collections.Generic;
using System.Text;namespace Iterator_GenreicsSample1
{
public class Stack<T>: IEnumerable<T>
{
T[] Items;
int count;
public void push(T value)
{
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 value = Items[count - 1];
count--;
} public IEnumerator<T> GetEnumerator()
{
for (int i = count - 1; i >= 0;--i )
{
yield return Items[i];
}
}
public IEnumerable<T> PoptoBottom
{
get{
return this;
}
} public IEnumerable<T> BottomtoPop
{
get{
for(int i=0;i<count;i++)
yield return Items[i];
}
}
}
class mytest
{
static void Main(string[] args)
{
Stack<int> myStack = new Stack<int>();
for (int i = 0; i < 10; i++) myStack.push(i);
foreach (int value in myStack.PoptoBottom) Console.Write("-- {0} ", value);
Console.WriteLine();
foreach (int value in myStack.BottomtoPop) Console.Write("-- {0} ", value);
Console.WriteLine();
Console.Read();
}
}
}
在2005编译时总是提示Stack类的方法
“GetEnumerator”有以下错误:
'Iterator_GenreicsSample1.Stack<T>' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'Iterator_GenreicsSample1.Stack<T>.GetEnumerator()' is either static, not public, or has the wrong return type. 我怀疑是第三种错误,即返回类型不正确,但不知道怎么修改,郁闷啊!请高手帮忙指点,谢谢!
using System.Collections.Generic;
using System.Text;namespace Iterator_GenreicsSample1
{
public class Stack<T>: IEnumerable<T>
{
T[] Items;
int count;
public void push(T value)
{
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 value = Items[count - 1];
count--;
} public IEnumerator<T> GetEnumerator()
{
for (int i = count - 1; i >= 0;--i )
{
yield return Items[i];
}
}
public IEnumerable<T> PoptoBottom
{
get{
return this;
}
} public IEnumerable<T> BottomtoPop
{
get{
for(int i=0;i<count;i++)
yield return Items[i];
}
}
}
class mytest
{
static void Main(string[] args)
{
Stack<int> myStack = new Stack<int>();
for (int i = 0; i < 10; i++) myStack.push(i);
foreach (int value in myStack.PoptoBottom) Console.Write("-- {0} ", value);
Console.WriteLine();
foreach (int value in myStack.BottomtoPop) Console.Write("-- {0} ", value);
Console.WriteLine();
Console.Read();
}
}
}
在2005编译时总是提示Stack类的方法
“GetEnumerator”有以下错误:
'Iterator_GenreicsSample1.Stack<T>' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'Iterator_GenreicsSample1.Stack<T>.GetEnumerator()' is either static, not public, or has the wrong return type. 我怀疑是第三种错误,即返回类型不正确,但不知道怎么修改,郁闷啊!请高手帮忙指点,谢谢!
解决方案 »
- 怎样disable某一列,使其值不能被修改?
- DataGridView验证问题,在CellEndEdit和CellValidating中验证都不能满足要求呢
- 散分兼发布一个图像处理小工具:Crimm Imageshop 2.0
- 封装 MessageBox.Show();
- 用.NET实现一卡通,探测之类的软件,有没有相关之类的好书?
- 求正则表达式
- 用C#写了一个模拟福利彩票双色球的选号程序,怎样实现当随机产生的6个蓝色号码出现重复号码就重新随机产生号码?
- 如何在以下SQL语句中添加查询条件在DutyDate的两个datetime之间
- 各位大哥!救命呀!asp应用程序的问题(inetinfo.exe cpu的占用率100%)
- 请问:如何读取一个XML文件,然后根据将该文件中需要的结点写入另一个XML文件?给分的哟!
- 如何得到DataGridView的列标题高度
- 一个关于日期型的SQL语句
{
return GetEnumerator();
}
{
return GetEnumerator();
}
{
return GetEnumerator();
}
为什么要这样写啊,
另外,在类中加上了这样的方法,但还是不成功,提示public Sysetm.Collections.IEnumerator Sysetm.Collections.IEnumerable.GetEnumerator()中类型public不合法.郁闷了,还请你再解释解释,谢谢!
去掉“public”就可以了,
搞定,谢谢了:)