Advertisement
Intermediate C#, Fundamentals

What is the difference between checked and unchecked arithmetic?

Short answer

By default integer overflow wraps silently (unchecked); a checked context throws OverflowException on overflow, which is safer for money/counters.

Save C# mock interview
Advertisement
AModel answer
By default integer overflow wraps silently (unchecked); a checked context throws OverflowException on overflow, which is safer for money/counters.
checked {
    int max = int.MaxValue;
    int y = max + 1; // throws OverflowException
}
Real-world example
Wrapping financial calculations in checked so a silent overflow never corrupts a balance.