unchecked C# with Example
The unchecked keyword prevents the compiler from checking for overﬂows/underﬂows.
const int ConstantMax = int.MaxValue;
int1 = 2147483647 + 10;
int1 = unchecked(ConstantMax + 10);
Without the unchecked keyword, neither of the two addition operations will compile.
When is this useful?
This is useful as it may help speed up calculations that deﬁnitely will not overﬂow since checking for overﬂow takes
time, or when an overﬂow/underﬂow is desired behavior (for instance, when generating a hash code).