分享
 
 
 

14.5.12 The checked & unchecked operators

王朝other·作者佚名  2006-02-28
窄屏简体版  字體: |||超大  

The checked and unchecked operators are used to control the overflow

checking context for integral-type

arithmetic operations and conversions.

checked-expression:

checked ( expression )

unchecked-expression:

unchecked ( expression )

The checked operator evaluates the contained expression in a checked

context, and the unchecked

operator evaluates the contained expression in an unchecked context. A

checked-expression or uncheckedexpression

corresponds exactly to a parenthesized-expression (?4.5.3), except that

the contained expression

is evaluated in the given overflow checking context.

The overflow checking context can also be controlled through the checked

and unchecked statements

(?5.11).

The following operations are affected by the overflow checking context

established by the checked and

unchecked operators and statements:

?The predefined ++ and -- unary operators (?4.5.9 and ?4.6.5), when the

operand is of an integral

type.

?The predefined - unary operator (?4.6.2), when the operand is of an

integral type.

?The predefined +, -, *, and / binary operators (?4.7), when both

operands are of integral types.

?Explicit numeric conversions (?3.2.1) from one integral type to another

integral type, or from float or

double to an integral type.

When one of the above operations produce a result that is too large to

represent in the destination type, the

context in which the operation is performed controls the resulting behavior:

?In a checked context, if the operation is a constant expression (?4.15),

a compile-time error occurs.

Otherwise, when the operation is performed at run-time, a

System.OverflowException is thrown.

?In an unchecked context, the result is truncated by discarding any

high-order bits that do not fit in the

destination type.

C# LANGUAGE SPECIFICATION

152

For non-constant expressions (expressions that are evaluated at run-time)

that are not enclosed by any

checked or unchecked operators or statements, the default overflow checking

context is unchecked,

unless external factors (such as compiler switches and execution

environment configuration) call for

checked evaluation.

For constant expressions (expressions that can be fully evaluated at

compile-time), the default overflow

checking context is always checked. Unless a constant expression is

explicitly placed in an unchecked

context, overflows that occur during the compile-time evaluation of the

expression always cause compiletime

errors.

[Note: Developers may benefit if they exercise their code using checked

mode (as well as unchecked mode).

It also seems reasonable that, unless otherwise requested, the default

overflow checking context is set to

checked when debugging is enabled. end note]

[Example: In the example

class Test

{

static readonly int x = 1000000;

static readonly int y = 1000000;

static int F() {

return checked(x * y); // Throws OverflowException

}

static int G() {

return unchecked(x * y); // Returns -727379968

}

static int H() {

return x * y; // Depends on default

}

}

no compile-time errors are reported since neither of the expressions can be

evaluated at compile-time. At

run-time, the F method throws a System.OverflowException, and the G method

returns ?727379968

(the lower 32 bits of the out-of-range result). The behavior of the H

method depends on the default overflow

checking context for the compilation, but it is either the same as F or the

same as G. end example]

[Example: In the example

class Test

{

const int x = 1000000;

const int y = 1000000;

static int F() {

return checked(x * y); // Compile error, overflow

}

static int G() {

return unchecked(x * y); // Returns -727379968

}

static int H() {

return x * y; // Compile error, overflow

}

}

the overflows that occur when evaluating the constant expressions in F and

H cause compile-time errors to

be reported because the expressions are evaluated in a checked context. An

overflow also occurs when

evaluating the constant expression in G, but since the evaluation takes

place in an unchecked context, the

overflow is not reported. end example]

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.

[Example: In the example

Chapter 14 Expressions

153

class Test

{

static int Multiply(int x, int y) {

return x * y;

}

static int F() {

return checked(Multiply(1000000, 1000000));

}

}

the use of checked in F does not affect the evaluation of x * y in

Multiply, so x * y is evaluated in

the default overflow checking context. end example]

The unchecked operator is convenient when writing constants of the signed

integral types in hexadecimal

notation. [Example: For example:

class Test

{

public const int AllBits = unchecked((int)0xFFFFFFFF);

public const int HighBit = unchecked((int)0x80000000);

}

Both of the hexadecimal constants above are of type uint. Because the

constants are outside the int range,

without the unchecked operator, the casts to int would produce compile-time

errors. end example]

[Note: The checked and unchecked operators and statements allow programmers

to control certain

aspects of some numeric calculations. However, the behavior of some numeric

operators depends on their

operands? data types. For example, multiplying two decimals always results

in an exception on overflow

even within an explicitly unchecked construct. Similarly, multiplying two

floats never results in an

exception on overflow even within an explicitly checked construct. In

addition, other operators are never

affected by the mode of checking, whether default or explicit. As a service

to programmers, it is

recommended that the compiler issue a warning when there is an arithmetic

expression within an explicitly

checked or unchecked context (by operator or statement) that cannot

possibly be affected by the specified

mode of checking. Since such a warning is not required, the compiler has

flexibility in determining the

circumstances that merit the issuance of such warnings. end note]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有