title: If statement appears to be evaluating even when condition evaluates to false
body: <p>Late At Work last night, we were trying to figure out why something was failing. A validation check was failing when it shouldn't have been.</p>
  
  <p>We ended up adding a print statement to this code (disassembled from Reflector in order to check that the code was actually what we had written):</p>
  
  <pre><code>public static string Redacted(string name, DateTime lastModified)
  {
      long ticks = lastModified.Ticks;
      if ((ticks != (ticks - (ticks % 10000L))) &amp;&amp;
              (lastModified != DateTime.MaxValue))
      {
          Log.Debug(string.Format("Last Modified Date = '{0}'. Ticks = '{1}'. TicksCalc = '{2}'",
              lastModified.ToString("dd/MM/yyyy hh:mm:ss.fff"),
              ticks, ticks - (ticks % 10000L)));
  </code></pre>
  
  <p>It printed (reformatted):</p>
  
  <pre><code>Last Modified Date = '22/03/2011 12:16:22.000'.
  Ticks     = '634363497820000000'.
  TicksCalc = '634363497820000000'            
  </code></pre>
  
  <p>But the condition is that "<code>ticks</code>" (which is equal to Ticks printed above) is not equal to "<code>(ticks - (ticks % 10000))</code>" (which is equal to TicksCalc)! 634363497820000000 != 634363497820000000?!</p>
  
  <p>In order to determine what is going on here, we added another two statements:</p>
  
  <pre><code>long ticks = lastModified.Ticks;
  /* Added following two lines: */
  long num2 = ticks - (ticks % 10000L);
  Log.Debug((ticks == num2).ToString());
  /* */
  if ((ticks != (ticks - (ticks % 10000L))) &amp;&amp;
          (lastModified != DateTime.MaxValue))
  {
      Log.Debug(string.Format("Last Modified Date = '{0}'. Ticks = '{1}'. TicksCalc = '{2}'",
          lastModified.ToString("dd/MM/yyyy hh:mm:ss.fff"),
          ticks, ticks - (ticks % 10000L)));
  </code></pre>
  
  <p>As it should have, this one printed <code>true</code> (when testing with the same value), and didn't write the second line.</p>
  
  <p>Feeling a bit lost, we then removed the two lines again, recompiled, and reran. The original behaviour repeated itself.</p>
  
  <p>This morning, <a href="http://www.youtube.com/watch?v=ExAqlqpwKWc" rel="nofollow">I recorded a video</a>.  </p>
  
  <p>The video first of all shows hitting a breakpoint in the method using the 'broken' code, then rebuilding and rerunning using the 'working' code. Note that even though the debugger displays that the <code>if</code> condition evaluates as to <code>false</code>, the body is still entered.</p>
  
  <p>I've seen things like this happen before when observed by the debugger, due to the debugger forcing some things to be evaluated, but this happens whether or not the debugger is employed.</p>
  
  <p>Furthermore, this only happens in Release mode (i.e. with JIT optimizations enabled).</p>
  
  <p>Here are the disassembled methods for both versions: <a href="http://pastebin.com/5ELMrS8X" rel="nofollow">working</a>, <a href="http://pastebin.com/ADnzsDst" rel="nofollow">not working</a>. I can't really read assembly, so am posting them here in the hopes of elucidation. </p>
  
  <p>I hope that the answer isn't something obvious that I've overlooked completely...!</p>
  
  <p>Edit: Here is the IL. I don't think there's anything wrong with it because it decompiles to the correct C#:</p>
  
  <ul>
  <li><a href="http://pastebin.com/K7Fi4S0i" rel="nofollow">Not working</a></li>
  <li><a href="http://pastebin.com/kmdDbJWx" rel="nofollow">Working</a></li>
  </ul>
  
  <p><strong>Update</strong>:</p>
  
  <p><a href="https://connect.microsoft.com/VisualStudio/feedback/details/671105/jitter-or-c-compiler-bug#details" rel="nofollow">Confirmed as a bug by Microsoft, to be fixed in the next release</a>.</p>