When must we use checked operator in C#?

前端 未结 4 1812
野趣味
野趣味 2021-01-04 05:22

When must we use checked operator in C#?
Is it only suitable for exception handling?

4条回答
  •  独厮守ぢ
    2021-01-04 05:34

    From The checked and unchecked operators

    The checked and unchecked operators are used to control the overflow checking context for integral-type arithmetic operations and conversions.

    In a checked context, if an expression produces a value that is outside the range of the destination type, the result depends on whether the expression is constant or non-constant. Constant expressions cause compile time errors, while non-constant expressions are evaluated at run time and raise exceptions.

    In an unchecked context, if an expression produces a value that is outside the range of the destination type, the result is truncated.

    checked, unchecked

提交回复
热议问题