I have a closed while loop inside the foreach loop, where I would like to increase the counter indefinitely while a certain condition is fulfilled. To do this, I try to list the enumerator in IEnumerator <T> (which should be if it is in the foreach loop), and then calls MoveNext () on the custom object, but it gives me an error saying that I cannot convert it.
It is not possible to convert the type "System.DateTime" to System.Collections.Generic.IEnumerator through a link conversion, box conversion, unbox conversion, conversion conversion, or conversion of the null type.
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 IEnumerator inside a foreach loop?
EDIT: Edited to show if there is code after a while loop that I would like to execute after the condition is fulfilled, so I wanted to manually increment it inside and then exit it, and not continue, which will bring me back up. If this is not possible, I find it best to redesign how I do it.
source share