using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Con_yield
{
class Program
{
static void Main(string[] args)
{
HelloCollection hellocollection = new HelloCollection();
//为什么默认的是GetEnumerator()方法
foreach (var item in hellocollection)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
class HelloCollection
{
public IEnumerator<string> GetEnumerator(string a, string b)
{
yield return "H";
yield return "w";
}
public IEnumerator<string> GetEnumerator()
{
yield return "Hello";
yield return "world";
}
}
}
//输出结果为
Hello
world
------解决思路----------------------
至于为什么是无参方法,因为foreach要的就是无参的,你可以试着把这个无参的删掉,你的程序应该会在运行时报错