Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
The operations that are affected by overflow checking using checked operators are those that use predefined operators, including "++", "--", and binary operators like "+", "-", "/", "*", and explicit numeric conversions from one integral type to another, or from float/double to integral type. The output of the operation will be based on the operands. For expressions that contain only constant values, overflow can be detected by the compiler and displayed as an error. For expressions consisting of one or more than one non-constant value, the overflow will be checked during runtime and an exception (System.OverflowException) will be raised.
In contrast to overflow checking for signed integer arithmetic in C/C++ in which it is "implementation defined", C# has improved the way overflow check is controlled. Checked operator is used to execute C# statements in a checked context such that an exception is raised when an arithmetic overflow occurs. It forces the Common Language Runtime (CLR) to handle stack overflow situations while performing operations on integer types that can result in values outside the bounds of the data type.
A checked operator affects the overflow checking context for operations that are textually specified within parentheses. It does not affect any function invoked as a result of the evaluation of the contained expression.