title: When cast NaN to int, why does the value change depending on whether it is assigned to a variable or not?
body: <p>Casting double.NaN to int will be 0.</p>
  
  <pre><code>Console.WriteLine(unchecked((int)double.NaN));    // 0
  </code></pre>
  
  <p><strong>However, assigning double.NaN to a variable and then casting to int will be  -2147483648.</strong></p>
  
  <pre><code>double value = double.NaN;
  Console.WriteLine (unchecked ((int) value));    // -2147483648
  Console.WriteLine ((int) value);                // -2147483648
  </code></pre>
  
  <p>Why does the result change depending on whether or not assign it to a variable?</p>
  
  <h2>C# Specification</h2>
  
  <p>6.2.1 Explicit numeric conversions</p>
  
  <ul>
  <li>In a checked context, the conversion proceeds as follows:
  
  <ul>
  <li>If the value of the operand is NaN or infinite, a System.OverflowException is thrown.</li>
  <li>Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.</li>
  <li>Otherwise, a System.OverflowException is thrown.</li>
  </ul></li>
  <li>In an unchecked context, the conversion always succeeds, and proceeds as follows.
  
  <ul>
  <li><strong>If the value of the operand is NaN or infinite, the result of the conversion is an unspecified value of the destination type.</strong></li>
  <li>Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.</li>
  <li>Otherwise, the result of the conversion is an unspecified value of the destination type.</li>
  </ul></li>
  </ul>
  
  <h2>Envrionment</h2>
  
  <ul>
  <li>Compiler: Visual Studio 2013</li>
  <li>Rimtime: .NET Framework 4.6.0</li>
  <li>OS: Windows 10 Version 1607</li>
  <li>CPU: Intel Core i7 920</li>
  </ul>
  
  <p>Windows:</p>
  
  <pre><code>Console.WriteLine(Environment.OSVersion);    // Microsoft Windows NT 6.2.9200.0
  Console.WriteLine(Environment.Version);      // 4.0.30319.42000
  </code></pre>