Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Many of the other answers recommend using <code>continue</code>, 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 <em>have</em> the enumerator, and that means writing your loop as a <code>while</code>.</p> <pre><code>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 } } </code></pre> <p>From your comments:</p> <blockquote> <p>How can the foreach loop advance it if it doesn't implement the IEnumerator interface?</p> </blockquote> <p>In your loop, <code>time</code> is a <code>DateTime</code>. It is not the object that needs to implement an interface or pattern to work in the loop. <code>times</code> is a sequence of <code>DateTime</code> values, it is the one that must implement the enumerable pattern. This is generally fulfilled by implementing the <code>IEnumerable&lt;T&gt;</code> and <code>IEnumerable</code> interfaces, which simply require <code>T GetEnumerator()</code> and <code>object GetEnumerator()</code> methods. The methods return an object implementing <code>IEnumerator&lt;T&gt;</code> and <code>IEnumerator</code>, which define a <code>bool MoveNext()</code> method and a <code>T</code> or <code>object Current</code> property. But <code>time</code> cannot be cast to <code>IEnumerator</code>, because it is no such thing, and neither is the <code>times</code> sequence.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload