迭代器 Iterator
我先贴上代码,
public class CheckBox : IEnumerable<string>
{
private string[] strings;
private int rtc;
/*
* 索引
*/
public string this[int index]
{
get
{
if (0 > index && index > 7)
{
return null;
}
else
{
return strings[index];
}
}
}
public CheckBox(params string[] InitString)
{
rtc = 0;
strings = new string[8];
foreach (string s in InitString)
{
strings[rtc++] = s;
}
}
public void Add(string theString)
{
if (rtc >= 0 && rtc <= 7)
{
strings[rtc++] = theString;
}
}
public int GetNumEnties()
{
return rtc;
}
public void Remove(string key)
{
ArrayList temp = new ArrayList();
temp.AddRange(strings);
temp.Remove(key);
strings = new string[8];
temp.CopyTo(strings);
}
public System.Collections.Generic.IEnumerator<string> GetEnumerator()
{
foreach (string s in strings)
{
yield return s;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
//return (IEnumerator)(new Object()); 不推荐强制转换的做法,
throw new NotImplementedException();
}
}
注意啦,一定要实现两个GetEnumerator(); 父类子类的 ;