手动增加foreach循环内的枚举 [英] Manually increment an enumerator inside foreach loop

查看:239
本文介绍了手动增加foreach循环内的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套而foreach循环,我想同时在一定条件满足indefinitately推进枚举内循环。要做到这一点,我试着铸造枚举的IEnumerator< T>(它必须是如果是在一个foreach循环),然后调用铸造对象的MoveNext(),但它给了我一个错误说我不能把它转换。



无法通过引用转换,装箱转换,取消装箱转换,包装转换或null类型转换类型'System.DateTime的'转换为System.Collections.Generic.IEnumerator。

 的foreach(DateTime的时间倍)
{
,而(情况):
{
//执行操作
//移动到下一个项目
(时间的IEnumerator<&日期时间GT)。的MoveNext(); //不会让我这样做
}

//代码,而满足条件后,
}


什么是手动增加的IEnumerator foreach循环内的最佳方式。



编辑:
编辑,表示有,一旦条件满足这就是为什么我要手动递增内部一会儿,然后打破它,而不是继续这将让我重新回到顶部我想执行while循环后面的代码。如果这是不可能的,我相信最好的办法是重新设计我过得怎么样了。


解决方案

很多其他的答案推荐使用继续,这很可能帮你做你需要做的事情。然而,在显示手动移动枚举,利益首先必须的有无的枚举,这意味着在编写循环为,而

 使用(VAR枚举= times.GetEnumerator())
{
DateTime的时间;
,而(enumerator.MoveNext())
{
时间= enumerator.Current;
//前提条件码
,而(情况):
{
如果(enumerator.MoveNext())
{
时间= enumerator.Current;
//条件码
}
,否则
{
条件= FALSE;
}
}
//后置条件码
}
}

从您的意见:




如何foreach循环前进,如果不实现IEnumerator接口?




在你的循环,时间日期时间。这并不是说需要实现一个接口或图案在循环工作的对象。 的DateTime 值序列,那就是必须实现枚举的模式之一。 T>这通常是通过实施的IEnumerable<履行的IEnumerable 接口,它只是需要 ŧ的GetEnumerator()对象的GetEnumerator()方法。该方法返回实施对象的IEnumerator< T> 的IEnumerator ,它定义了一个 BOOL的MoveNext ()方法和 T 对象电流属性。但时间不能转换到的IEnumerator ,因为它是没有这样的事情,也不是序列。


I have a nested while loop inside a foreach loop where I would like to advance the enumerator indefinitately while a certain condition is met. To do this I try casting the enumerator to IEnumerator< T > (which it must be if it is in a foreach loop) then calling MoveNext() on the casted object but it gives me an error saying I cannot convert it.

Cannot convert type 'System.DateTime' to System.Collections.Generic.IEnumerator via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.

        foreach (DateTime time in times)
        {
            while (condition)
            {
                // perform action
                // move to next item
                (time as IEnumerator<DateTime>).MoveNext(); // will not let me do this
            }

            // code to execute after while condition is met
         }

What is the best way to manually increment the IEnumerator inside of the foreach loop?

EDIT: Edited to show there is code after the while loop that I would like executed once the condition is met which is why I wanted to manually increment inside the while then break out of it as opposed to continue which would put me back at the top. If this isn't possible I believe the best thing is to redesign how I am doing it.

解决方案

Many of the other answers recommend using continue, which may very well help you do what you need to do. However, in the interests of showing manually moving the enumerator, first you must have the enumerator, and that means writing your loop as a while.

using (var enumerator = times.GetEnumerator())
{
    DateTime time;
    while (enumerator.MoveNext()) 
    {
        time = enumerator.Current;
        // pre-condition code
        while (condition)
        {
            if (enumerator.MoveNext())
            {
                time = enumerator.Current;
                // condition code
            }
            else 
            {
                condition = false;
            }
        }
        // post-condition code
    }
}

From your comments:

How can the foreach loop advance it if it doesn't implement the IEnumerator interface?

In your loop, time is a DateTime. It is not the object that needs to implement an interface or pattern to work in the loop. times is a sequence of DateTime values, it is the one that must implement the enumerable pattern. This is generally fulfilled by implementing the IEnumerable<T> and IEnumerable interfaces, which simply require T GetEnumerator() and object GetEnumerator() methods. The methods return an object implementing IEnumerator<T> and IEnumerator, which define a bool MoveNext() method and a T or object Current property. But time cannot be cast to IEnumerator, because it is no such thing, and neither is the times sequence.

这篇关于手动增加foreach循环内的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆