【发布时间】:2010-12-28 18:38:40
【问题描述】:
我使用以下代码使 myClass 能够使用 foreach。但是我对编程比较陌生,并且在理解以下代码方面有些困难。我在 cmets 中描述了我的问题。如果能提供一些信息,我将不胜感激。
public class MyClass : IEnumerable<string>
{
//1) What is IEnumerator for?
// Whats the difference between IEnumerator and IEnumerable
public IEnumerator<string> GetEnumerator()
{
yield return "first";
yield return "second";
}
//2) What is it for? It just calls above method
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
//3) Lastly what benefits I have from implementing genetic interface
//IEnumerable<string> instead of just IEnumerable
【问题讨论】:
-
我会让 john skeet 回答这个问题
-
@Hasan:我担心他拼错名字会对你做什么。
标签: c# .net ienumerable ienumerator