title: Are explicitly Infinite Loops handled in .NET as a special case?
body: <p>Earlier today, as I was coding a method and it struck me that I wasn't sure exactly why the idiom I was implementing compiles.  If everything else is abstracted away, it would look something like this:</p>
  
  <pre><code>    private int Example()
      {
          while (true)
          {
              if (some condition)
              {
                 return 1;
              }
          }
      }
  </code></pre>
  
  <p>You have an explicitly infinite loop, and some set of conditions inside the loop that cause the loop to end with a return statement.  Let's ignore for the time being why I was doing this as opposed to checking for a termination condition in the while clause as the answer is convoluted and irrelevant -- what I want to know is why the compiler doesn't flag this with a "Not all paths return a value." error, as, strictly speaking not all paths do return a value.  The case in which the while loop is never entered (which, of course, never happens) doesn't return anything.</p>
  
  <p>Now, there are two reasons I can imagine it happening: this is a side effect of optimization that's occurring for other reasons, or this case is explicitly being handled by the compiler to allow this idiom.  My instinct is that it's probably the first case.  It doesn't surprise me at all, for instance, that this compiles:</p>
  
  <pre><code>private int Example2()
  {
     if (true) return 1;
  }
  </code></pre>
  
  <p>Because the compiler sees a constant true in the if, and optimizes the conditional away.  I don't really get why this would "fix" the first example, though.  </p>
  
  <p>Oh, and even more weirdly, if some optimization that gets rid of the loop is in play, this compiles:</p>
  
  <pre><code>    private int Example3()
      {
          while (true)
          {
              if (false)
              {
                 return 1;
              }
          }
      }
  </code></pre>
  
  <p>I would think that the entire inner loop would be optimized away, getting rid of all of the valid returns. What's actually going on here at the bytecode/compiler level that makes this all make sense?</p>