Bagikan melalui


Checked and Unchecked (C# Reference)

C# statements can execute in either checked or unchecked context. In a checked context, arithmetic overflow raises an exception. In an unchecked context, arithmetic overflow is ignored and the result is truncated.

  • checked   Specify checked context.

  • unchecked   Specify unchecked context.

If neither checked nor unchecked is specified, the default context depends on external factors such as compiler options.

The following operations are affected by the overflow checking:

  • Expressions using the following predefined operators on integral types:

    ++   —   - (unary)   +   -   *   /

  • Explicit numeric conversions between integral types.

The /checked compiler option lets you specify checked or unchecked context for all integer arithmetic statements that are not explicitly in the scope of a checked or unchecked keyword.

See Also

Reference

C# Keywords

Statement Keywords (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference