Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>All my problems would be solved if there was a function like:</p> <p><code>_nextChar = getAlphabet().moveNext() //or getNext()</code></p> </blockquote> <p>There is a function <strong>exactly</strong> like that. It just belongs to <code>IEnumerator&lt;T&gt;</code>, not <code>IEnumerable&lt;T&gt;</code>!</p> <pre><code>private char _nextChar; private IEnumerable&lt;char&gt; getAlphabet() { yield return 'A'; yield return 'B'; yield return 'C'; } public void sortAlphabet() { using (var enumerator = getAlphabet().GetEnumerator()) { while (enumerator.MoveNext()) { char alpha = enumerator.Current; switch (alpha) { case 'A': if (enumerator.MoveNext()) { _nextChar = enumerator.Currrent; } else { // You decide what to do in this case. } break; case 'B': // etc. break; } } } } </code></pre> <p>Here's a question for you, though. Is it necessary that this code use an <code>IEnumerable&lt;char&gt;</code>, rather than an <code>IList&lt;char&gt;</code>? I ask because, as if this weren't obvious, the code would be much simpler if you had random access to the items returned by <code>getAlphabet</code> by index (and if someone is tempted to point out that you can do this with <a href="http://msdn.microsoft.com/en-us/library/bb299233.aspx" rel="noreferrer"><code>ElementAt</code></a>, please, just get that idea out of your head right now).</p> <p>I mean, consider what the code would look like in this case:</p> <pre><code>private char _nextChar; private IList&lt;char&gt; getAlphabet() { return Array.AsReadOnly(new[] { 'A', 'B', 'C' }); } public void sortAlphabet() { IList&lt;char&gt; alphabet = getAlphabet(); for (int i = 0; i &lt; alphabet.Count - 1; ++i) { char alpha = alphabet[i]; switch (alpha) { case 'A': _nextChar = alphabet[i + 1]; break; case 'B': // etc. break; } } } </code></pre> <p>Isn't that much easier?</p>
 

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