Can somebody please explain the following behavior:
static void Main(string[] args)
{
checked
{
double d = -1d + long.MinValue; //this resolves at runtime to -9223372036854780000.00
//long obviousOverflow = -9223372036854780000; //compile time error, '-' cannot be applied to operand of tpye ulong -> this makes it obvious that -9223372036854780000 overflows a long.
double one = 1;
long lMax = (long)(one + long.MaxValue); // THROWS
long lMin = (long)(-one + long.MinValue); // THEN WHY DOES THIS NOT THROW?
}
}
I don't undersant why I'm not getting an OverFlowException
in the last line of code.
UPDATE Updated code to make it obvious that checked does through when casting a double to long except in the last case.
See Question&Answers more detail:os