C#检查块 [英] C# checked block

查看:114
本文介绍了C#检查块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能向我解释,究竟是什么在检查选中块?结果
当我应该使用每个?

Can someone explain to me what exactly is the checked an unchecked block ?
And when should I use each ?

推荐答案

算术溢出;例如:

int i = int.MaxValue -10;
checked {       
   i+= 20; // boom: OverflowException
           // "Arithmetic operation resulted in an overflow."
}



因此,使用检查当您不想要的偶然溢/环绕式是一个问题,而宁愿看到一个例外。

So use checked when you don't want accidental overflow / wrap-around to be a problem, and would rather see an exception.

选中明确设置模式以允许溢出;默认为选中除非你告诉编译器,否则 - 无论是通过代码(以上)或编译器开关( /检查在CSC)。

unchecked explicitly sets the mode to allow overflow; the default is unchecked unless you tell the compiler otherwise - either through code (above) or a compiler switch (/checked in csc).

这篇关于C#检查块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆