当必须使用我们检查经营者在C#中? [英] When must we use checked operator in C#?

查看:93
本文介绍了当必须使用我们检查经营者在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们何时必须在C#中使用检查经营者?难道只适合异常处理?

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

推荐答案

您会使用检查,以防范(静音)溢出在表达式中。结果
和使用选中当你知道会发生一种无害的溢出。

You would use checkedto guard against a (silent) overflow in an expression.
And use unchecked when you know a harmless overflow might occur.

您使用无论是在地方,你不希望依赖于默认的(项目范围)编译器设置。

You use both at places where you don't want to rely on the default (project-wide) compiler setting.

这两种形式是相当罕见的,但这样做的关键整数运算时,这是值得思考可能的溢出

Both forms are pretty rare, but when doing critical integer arithmetic it is worth thinking about possible overflow.

另外请注意,他们有两种形式:

Also note that they come in two forms:

 x = unchecked(x + 1);    // ( expression )
 unchecked { x = x + 1;}  // { statement(s) }

这篇关于当必须使用我们检查经营者在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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