Skip to content

Overflow Checking

Mario Gutierrez edited this page Jan 7, 2017 · 1 revision

You can use the checked keyword to carry out a checked cast, which checks for numeric overflow when narrowing a scope.

Block form:

checked
{
  byte a = (byte)someIntegerA;
  byte b = (byte)someIntegerB;
}

Inline form:

checked(byte a = (byte)someIntegerA);

If you can enabled overflow checking project-wide, you can use the unchecked keyword in a similar fashion to ignore checking.

The project-wide setting is usually in "Build Settings > Check for arithmetic overflow/underflow".

Clone this wiki locally