创建迭代器最常用的方法是对IEnumerator接口实现GetEnumerator方法。
public class Family : System.Collections.IEnumerable
{
string[] myFamily = {“父亲”,”母亲”,”弟弟”,”妹妹”};
public System.Collections.IEnumerator GetEnumerator()
{
for(int i=0;i<myFamily.Length;i++)
{
yield return myFamily[i];
}
}
}
private void Form_Load(object sender, EventArgs e)
{
Family family = new Family();
foreach(string str in family)
{
}
}
《C#从入门到精通》读后总结
时间: 2024-10-24 05:26:28