我使用以下代码使myClass使用foreach.但我对编程很新,并且在理解下面的代码时遇到了一些困难.我在评论中描述了我的问题.我很感激提供一些信息.
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
Run Code Online (Sandbox Code Playgroud)