title: Explicit casting of double to int with different results
body: <p>We are using an API that basically does this</p>
  
  <pre><code>var t = TimeSpan.MaxValue;
  int x = (int)t.TotalMilliseconds;
  </code></pre>
  
  <p>where x will end up being passed to System.Threading.WaitHandle.WaitOne(int). </p>
  
  <p>The problem is when this code runs in our dev and staging environments, it doesn't throw any errors but when it runs in production it does throw:</p>
  
  <pre><code>Exception: System.ArgumentOutOfRangeException
  Message: Number must be either non-negative and less than or equal to Int32.MaxValue or -1.
  Parameter name: millisecondsTimeout
  </code></pre>
  
  <p>When I test this with a simple console app (both x86 and x64) the result of x = -2147483648 (int.MinValue) while when I run the code in the immediate window I get x = 1566804069. </p>
  
  <p>What is going on?</p>
  
  <p>Note: staging and production are all cloned from a single VM so there are no differences between them</p>
  
  <p><strong>THIS IS CODE THAT WE CANNOT CHANGE!</strong> Otherwise I wouldn't be asking this question.</p>