title: C# Code Simplification Query: The Sequential Foreach Loops
body: <p>Suppose I have some code that looks like this:</p>
  
  <pre><code>foreach(type x in list y)
  {
     //dostuff1(x)
  }
  
  foreach(type x in list y)
  {
     //dostuff2(x)
  }
  
  foreach(type x in list y)
  {
     //dostuff3(x)
  }
  
  foreach(type x in list y)
  {
     //dostuff4(x)
  }
  
  foreach(type x in list y)
  {
     //dostuff5(x)
  }
  </code></pre>
  
  <p>I cannot combine things into one big for loop like this:</p>
  
  <pre><code>foreach (type x in list y)
  {
      //dostuff1(x)
      //dostuff2(x)
      //dostuff3(x)
      //dostuff4(x)
      //dostuff5(x)
  }
  </code></pre>
  
  <p>Doing so would change the order.  Any commentary on the best ways to make the code simpler in C#?</p>
  
  <p>I imagine I could solve this problem by creating a function like this, though I'd rather leave it the way it is than force future readers of my code to understand <code>yield</code>:</p>
  
  <pre><code>void func(type x)
  {
      dostuff1(x)
      yield 0;
      dostuff2(x)
      yield 0;
      dostuff3(x)
      yield 0;
      dostuff4(x)
      yield 0;
      dostuff5(x)
      yield break;
  }
  
  for (int i = 0; i&lt;5; ++i)
  {
     foreach (type x in list y)
     {
         //Call func(x) using yield semantics, which I'm not going to look up right now
     }
  }
  </code></pre>