title: Why do we need two interfaces to enumerate a collection?
body: <p>It is quite a while that I have been trying to understand the idea behind <code>IEnumerable</code> and <code>IEnumerator</code>. I read all the questions and answers I could find over the net, and on StackOverflow in particular, but I am not satisfied. I got to the point where I understand <em>how</em> those interfaces should be used, but not <em>why</em> they are used this way.</p>
  
  <p>I think that the essence of my misunderstanding is that we need two interfaces for one operation. I realized that if both are needed, one was probably not enough. So I took the "hard coded" equivalent of <code>foreach</code> (as I found <a href="https://stackoverflow.com/a/7310570/1925272">here</a>):</p>
  
  <pre><code>while (enumerator.MoveNext())
  {
      object item = enumerator.Current;
  
      // logic
  }
  </code></pre>
  
  <p>and tried to get it to work with one interface, thinking something would go wrong which would make me understand why another interface is needed.</p>
  
  <p>So I created a collection class, and implemented <code>IForeachable</code>:</p>
  
  <pre><code>class Collection : IForeachable
  {
      private int[] array = { 1, 2, 3, 4, 5 };
      private int index = -1;
  
      public int Current =&gt; array[index];
  
      public bool MoveNext()
      {
          if (index &lt; array.Length - 1)
          {
              index++;
              return true;
          }
  
          index = -1;
          return false;
      }
  }
  </code></pre>
  
  <p>and used the <code>foreach</code> equivalent to nominate the collection:</p>
  
  <pre><code>var collection = new Collection();
  
  while (collection.MoveNext())
  {
      object item = collection.Current;
  
      Console.WriteLine(item);
  }
  </code></pre>
  
  <p>And it works! So what is missing here that make another interface required?</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p><strong>Edit:</strong>
  My question is not a duplicate of the questions listed in the comments:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/13902310/why-do-we-need-ienumerator-and-ienumerable/13960970#13960970">This</a> question is why interfaces are needed for enumerating in the first place.</li>
  <li><a href="https://stackoverflow.com/questions/619564/what-is-the-difference-between-ienumerator-and-ienumerable">This</a> question and <a href="https://stackoverflow.com/questions/558304/can-anyone-explain-ienumerable-and-ienumerator-to-me">this</a> question are about <em>what</em> are those interfaces and <em>how</em> should they be used.</li>
  </ul>
  
  <p>My question is <em>why</em> they are designed the way they are, not <em>what</em> are they, <em>how</em> they work, and why do we need them in the first place.</p>