Check vs unchecked

From the C# specification:
The following operations are affected by the overflow checking context established by the checked and unchecked operators and statements:

  • The predefined ++ and -- unary operators (§‎7.5.9 and §‎7.6.5), when the operand is of an integral type.
  • The predefined - unary operator (§‎7.6.2), when the operand is of an integral type.
  • The predefined +, -, *, and / binary operators (§‎7.7), when both operands are of integral types.
  • Explicit numeric conversions (§‎6.2.1) from one integral type to another integral type, or from float or double to an integral type.

The checked and unchecked operators only affect the overflow checking context for those operations that are textually contained within the "(" and ")" tokens. The operators have no effect on function members that are invoked as a result of evaluating the contained expression.

 

This means you'll still get an overflow exception with following code even if you specify unchecked explicitly (Make sure you are building with /checked):

 


In short unchecked is to supress overflow checking for the operators mentioned above. It is not something special to catch the OverFlowException! In IL you can see that if you are using checked we'll use special instructions (*.ovf*).

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值