当我通过比较来迭代两个具有不同类型的可枚举时,我发现了这一点:
class Program
{
public static IEnumerable<mm> YieldlyGet()
{
yield return new mm { Int = 0 };
yield return new mm { Int = 1 };
yield return new mm { Int = 2 };
yield return new mm { Int = 3 };
yield return new mm { Int = 4 };
yield return new mm { Int = 5 };
}
public static IEnumerable<int> YieldlyGetInt()
{
yield return 0;
yield return 1;
yield return 2;
yield return 3;
yield return 4;
yield return 5;
}
public static IEnumerable<int> Get()
{
return new List<int> { 0, 1,2,3,4,5 };
}
static void Main(string[] args)
{
var yieldr = YieldlyGet().GetEnumerator();
var yieldv = YieldlyGetInt().GetEnumerator();
var list = Get().GetEnumerator();
int i = -1;
Console.WriteLine("For the current index: {0}", ++i);
Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);
Console.WriteLine("For the current index: {0}", ++i);
Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);
Console.WriteLine("For the current index: {0}", ++i);
Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);
Console.WriteLine("For the current index: {0}", ++i);
Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);
Console.WriteLine("For the current index: {0}", ++i);
Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);
Console.WriteLine("For the current index: {0}", ++i);
Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);
Console.WriteLine("For the current index: {0}", ++i);
Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);
Console.ReadLine();
}
问题是,当我在最后一个位置之后,列表为我提供了默认值,而 yield 创建的迭代器继续显示最后一个值。
对于当前指数:6
年:我应该下一步移动吗?False,如果是,值:5
yv:我应该下一步移动吗?False,如果是,值:5
l:我应该下一步移动吗?假,如果是,值:0
为什么?