Back to results for “Fundamentals”
C# › Fundamentals

What is the difference between checked and unchecked arithmetic?

Intermediate

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.