Operations should not overflow
Arithmetic operations should not be performed without first validating the operands to make sure the result of the operation is not outside the range of possible values for the data types involved. Depending on the execution context and the data types involved, arithmetic overflow can result in either aor the most significant bits of the result discarded.
The following example shows a method that contains an operation that manipulates an integer that violates this rule. Visual Basic requires the Remove integer overflow option to be disabled for this to fire.
In the above example, if Int32.MinValue was passed for input parameter. Then the operation would underflow causing the most significant bit of the result to be discarded.
The following example shows this.
The following example fixes the above violation by validating the value of input.
The following example fixes the above violation by wrapping the operation in a checked block.
When the above operation overflows, a System.OverflowException will be thrown.
Note: There is no checked block equivalent for Visual Basic