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)
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.