C# Feature - checked operators
Overview
Section titled “Overview”The checked keyword and checked context make arithmetic overflow throw an exception instead of wrapping silently.
Introduced In
Section titled “Introduced In”C# 1.0 (2002)
Before
Section titled “Before”int max = int.MaxValue;int wrapped = max + 1;int max = int.MaxValue;int safe = checked(max + 1);Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Use
checkedin numeric code where overflow is a bug. - Use
uncheckedexplicitly when wraparound is intentional.