title: How to access the next and previous items within a foreach iteration over an IEnumerable?
body: <p>I want to access the next and previous items (if within range) while processing the current item and iterating over <code>IEnumerable&lt;object&gt;</code>, which lacks an indexer (as far as I know).</p>
  
  <p>I wouldn't want to require a <code>List&lt;object&gt;</code> collection instead or an array in order to accomplish this, although it's the only option that I know of right now.</p>
  
  <p>I'd also prefer to avoid Linq expressions if possible.</p>
  
  <p>That's how my method is defined at the moment:</p>
  
  <pre><code>public static void PrintTokens(IEnumerable&lt;object&gt; tokens)
  {
      foreach (var token in tokens)
      {
          // var next = ?
          // var prev = ?
      }
  }
  </code></pre>