Assuming default arithmetic overflow (not)checking, the following code
Action<Int32[]> action;
checked
{
action = array =>
Console.WriteLine(array[0] + array[1]);
}
var items = new[]
{
Int32.MaxValue,
Int32.MaxValue
};
action(items);
will result in
System.OverflowException: Arithmetic operation resulted in an overflow..
If we set project settings to /checked, and replace checked {
with unchecked {
, the exception won't be thrown.
So, can we rely on this behavior, or is it safer to array => unchecked (array[0] + array[1])
?