I was testing something out using LinqPad and was surprised that the following code did not produce an exception:
ulong lSmallValue = 5;
ulong lBigValue = 10;
ulong lDifference = lSmallValue - lBigValue;
Console.WriteLine(lDifference);
Console.WriteLine((long)lDifference);
This produces the following output:
18446744073709551611
-5
Fortunately, I was hoping for this behavior, but I was under the assumption that this would cause an OverflowException
to be thrown.
From System.OverflowException
:
An OverflowException is thrown at run time under the following conditions:
- An arithmetic operation produces a result that is outside the range of the data type returned by the operation.
- A casting or conversion operation attempts to perform a narrowing conversion, and the value of the source data type is outside the range of the target data type.
Why doesn't the operation lSmallValue - lBigValue
fall into the first category?