C#集合与接口(二)IEnumerable<T>接口
//字符中迭代
public class ListBoxTest: IEnumerable<String>
{
private string[] strings;
private int ctr = 0;
//可枚举的类可以返回枚举
public IEnumerator<string> GetEnumerator()
{
foreach (string s in strings)
{
yield return s;//返回可枚举对象
}
}
public ListBoxTest(params string[] initStrings)
{
strings = new string[initStrings.Length];
foreach(string s in initStrings)
{
strings[ctr++] = s;
}
}
public void Add(string s){
strings[ctr] = s;
ctr++;
}
public string this[int index]{
get{
if(index < 0) || (index > strings.Length)
{
throw new IndexOutOfRangeException();
}
return strings[index]
}
set
{
strings[index] =value;
}
}
public int GetStringCount(){
return ctr;
}
}
编辑器加载中...
posted on 2011-10-09 20:45 ManLoveGirls 阅读(327) 评论(0) 编辑 收藏 举报