📄 14.5.12.htm
字号:
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Hyperlinked ECMA C# Language Specification</title><meta name="author" content="Jon Jagger" /><link rel="stylesheet" href="ecma334.css"></link></head><body><div align="right"><em><a href="http://www.jaggersoft.com">Jon Jagger</a></em></div><div align="right"><a href="mailto:jon@jaggersoft.com">jon@jaggersoft.com</a></div><form method="get" action="http://search.atomz.com/search/"><input size="30" name="sp-q"></input><input type="submit" value="Search C# Spec"></input><input type="hidden" name="sp-a" value="sp10024177"></input><input type="hidden" name="sp-f" value="ISO-8859-1"></input></form><a href="toc.htm">Table of Contents</a> <a href="1.htm">1</a> <a href="2.htm">2</a> <a href="3.htm">3</a> <a href="4.htm">4</a> <a href="5.htm">5</a> <a href="6.htm">6</a> <a href="7.htm">7</a> <a href="8.htm">8</a> <a href="9.htm">9</a> <a href="10.htm">10</a> <a href="11.htm">11</a> <a href="12.htm">12</a> <a href="13.htm">13</a> <a href="14.htm">14</a> <a href="15.htm">15</a> <a href="16.htm">16</a> <a href="17.htm">17</a> <a href="18.htm">18</a> <a href="19.htm">19</a> <a href="20.htm">20</a> <a href="21.htm">21</a> <a href="22.htm">22</a> <a href="23.htm">23</a> <a href="24.htm">24</a> <a href="25.htm">25</a> <a href="notes.htm">Notes</a> <a href="HyperlinkedCSharpECMA.zip">Download</a><span class="ruler"></span><span class="heading">ECMA-334 C# Language Specification</span><span class="navigate"><a href="14.5.11.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="14.6.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="13.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="15.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="14.htm">14</a></span><span class="clause-title-previous"> Expressions</span></span><span class="clause-depth"><a href="14.4.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="14.6.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="14.5.htm">14.5</a></span><span class="clause-title-previous"> Primary expressions</span></span><span class="clause-depth"><a href="14.5.11.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="14.5.12.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">14.5.12</span><span class="clause-title"> The checked and unchecked operators</span></span><span class="locator">
Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>The checked and unchecked operators are used to control the overflow checking context for <span class="non-terminal"><a href="11.1.htm#integral-type">integral-type</a></span> arithmetic operations and conversions.</span> <span class="grammar-production"><span class="name"><a name="checked-expression"></a>checked-expression</span> : <span class="rhs"><span class="keyword">checked</span> <span class="terminal">(</span> <span class="non-terminal"><a href="14.14.htm#expression">expression</a></span> <span class="terminal">)</span> </span></span><span class="grammar-production"><span class="name"><a name="unchecked-expression"></a>unchecked-expression</span> : <span class="rhs"><span class="keyword">unchecked</span> <span class="terminal">(</span> <span class="non-terminal"><a href="14.14.htm#expression">expression</a></span> <span class="terminal">)</span> </span></span></span><span class="locator">
Paragraph 2</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P2S1"></a>The checked operator evaluates the contained expression in a checked context, and the unchecked operator evaluates the contained expression in an unchecked context.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>A <span class="non-terminal"><a href="14.5.12.htm#checked-expression">checked-expression</a></span> or <span class="non-terminal"><a href="14.5.12.htm#unchecked-expression">unchecked-expression</a></span> corresponds exactly to a <span class="non-terminal"><a href="14.5.3.htm#parenthesized-expression">parenthesized-expression</a></span> (<a href="14.5.3.htm">§14.5.3</a>), except that the contained expression is evaluated in the given overflow checking context.</span> </span><span class="locator">
Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>The overflow checking context can also be controlled through the checked and unchecked statements (<a href="15.11.htm">§15.11</a>).</span> </span><span class="locator">
Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>The following operations are affected by the overflow checking context established by the checked and unchecked operators and statements: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a> The predefined ++ and --unary operators (<a href="14.5.9.htm">§14.5.9</a> and <a href="14.6.5.htm">§14.6.5</a>), when the operand is of an integral type.</span> </li><li><span class="sentence"><span class="sentence-number">3</span> <a name="P4S3"></a> The predefined -unary operator (<a href="14.6.2.htm">§14.6.2</a>), when the operand is of an integral type.</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P4S4"></a> The predefined +, -, *, and / binary operators (<a href="14.7.htm">§14.7</a>), when both operands are of integral types.</span> </li><li><span class="sentence"><span class="sentence-number">5</span> <a name="P4S5"></a> Explicit numeric conversions (<a href="13.2.1.htm">§13.2.1</a>) from one integral type to another integral type.</span> </li></ul></span><span class="locator">
Paragraph 5</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P5S1"></a>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: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P5S2"></a> In a checked context, if the operation is a constant expression (<a href="14.15.htm">§14.15</a>), a compile-time error occurs.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P5S3"></a>Otherwise, when the operation is performed at run-time, a System.OverflowException is thrown.</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P5S4"></a> In an unchecked context, the result is truncated by discarding any high-order bits that do not fit in the destination type.</span> </li></ul></span><span class="locator">
Paragraph 6</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P6S1"></a>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.</span> </span><span class="locator">
Paragraph 7</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P7S1"></a>For constant expressions (expressions that can be fully evaluated at compile-time), the default overflow checking context is always checked.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P7S2"></a>Unless a constant expression is explicitly placed in an unchecked context, overflows that occur during the compile-time evaluation of the expression always cause compile-time errors.</span> </span><span class="paragraph"><span class="note">[Note: Developers may benefit if they exercise their code using checked mode (as well as unchecked mode). </span></span><span class="paragraph"><span class="note">It also seems reasonable that, unless otherwise requested, the default overflow checking context is set to checked when debugging is enabled. end note]</span> </span><span class="paragraph"><span class="example">[Example: In the example <pre class="code-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
}
}
</pre>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]</span> </span><span class="paragraph"><span class="example">[Example: In the example <pre class="code-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
}
}
</pre>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]</span> </span><span class="locator">
Paragraph 8</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P8S1"></a>The checked and unchecked operators only affect the overflow checking context for those operations that are textually contained within the "(" and ")" tokens.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P8S2"></a>The operators have no effect on function members that are invoked as a result of evaluating the contained expression.</span> <span class="example">[Example: In the example <pre class="code-example">
class Test
{
static int Multiply(int x, int y) {
return x * y;
}
static int F() {
return checked(Multiply(1000000, 1000000));
}
}
</pre>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]</span> </span><span class="locator">
Paragraph 9</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P9S1"></a>The unchecked operator is convenient when writing constants of the signed integral types in hexadecimal notation.</span> <span class="example">[Example: For example: <pre class="code-example">
class Test
{
public const int AllBits = unchecked((int)0xFFFFFFFF);
public const int HighBit = unchecked((int)0x80000000);
}
</pre></span></span><span class="paragraph"><span class="example">Both of the hexadecimal constants above are of type <span class="keyword">uint</span>. Because the constants are outside the <span class="keyword">int</span> range, without the unchecked operator, the casts to <span class="keyword">int</span> would produce compile-time errors. end example]</span> </span><span class="paragraph"><span class="note">[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]</span> </span><span class="ruler"></span><table><tr><td><table align="left" bgcolor="navy"><tr bgcolor="navy"><td><font face="Arial,sans-serif" size="6" color="yellow"><strong>{ JSL }</strong></font></td></tr></table></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Jagger Software Ltd</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Company # 4070126</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>VAT # 762 5213 42</strong></font></td></tr></table><img src="valid-html401.png" align="left" height="31" width="88" alt="Valid HTML 4.01" /><img src="vcss.gif" align="left" height="31" width="88" alt="Valid CSS" /></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -