Back to questions
JuniorTheoryCommon
What does the program print?
int max = int.MaxValue;
Console.WriteLine(max + 1);-2147483648
- Expecting
2147483648:max + 1is aninttoo, and anintcannot hold that number, so the value wraps around toint.MinValue. - Expecting an
OverflowException: arithmetic on variables is unchecked by default, and onlycheckedmakes an overflow throw.
- What happens if you write
checked(max + 1)instead? - Why does
Console.WriteLine(int.MaxValue + 1);fail to compile?