title: C# loop over bool values
body: <p>Is there a concise way to loop over true/false in C#?</p>
  
  <p>I have ~20 lines of code in a unit test I'd rather not duplicate to toggle one boolean true/false.</p>
  
  <p>I could break it off into a function and call it twice, but meh. This code <em>feels</em> more like I'm iterating over possible values than performing a distinct action with different parameters. Even if I had a function, I'd prefer the syntax of looping over the possible values rather than just calling it twice.</p>
  
  <p>I could write a <code>for</code> loop like so...</p>
  
  <pre><code>bool toggle;
  for (int i = 0; i &lt; 2; i++)
  {
      toggle = i == 1;
  }
  </code></pre>
  
  <p>But that doesn't seem very clean.</p>
  
  <p>I like <a href="https://stackoverflow.com/a/8805221/684776">this syntax</a>:</p>
  
  <pre><code>for (bool b : { false, true }) { /* ... */ }
  </code></pre>
  
  <p>But it doesn't look like that will compile in C#.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Following Jeroen's suggestion about local functions and Dmitry's answer, this is the route I went:</p>
  
  <pre><code>[TestMethod]
  public void GetAndSetValue()
  {
      foreach (bool toggle in new [] { false, true })
      {
          GetAndSetValue(toggle);
      }
  
      void GetAndSetValue(bool toggle)
      {
          // details not important
      }
  }
  </code></pre>
  
  <p>Reasonable coders can debate whether the loop reads more easily than two function calls:</p>
  
  <pre><code>GetAndSetValue(false);
  GetAndSetValue(true);
  </code></pre>
  
  <p>I like the loop better, so I'll roll with it until someone complains. Cheers!</p>