13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 14 Expressionsclass 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 inthe default overflow checking context. end example]The unchecked operator is convenient when writing constants of the signed integral types in hexadecimalnotation. [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 certainaspects of some numeric calculations. However, the behavior of some numeric operators depends on theiroperands’ data types. For example, multiplying two decimals always results in an exception on overfloweven within an explicitly unchecked construct. Similarly, multiplying two floats never results in anexception on overflow even within an explicitly checked construct. In addition, other operators are neveraffected by the mode of checking, whether default or explicit. As a service to programmers, it isrecommended that the compiler issue a warning when there is an arithmetic expression within an explicitlychecked or unchecked context (by operator or statement) that cannot possibly be affected by the specifiedmode of checking. Since such a warning is not required, the compiler has flexibility in determining thecircumstances that merit the issuance of such warnings. end note]14.6 Unary expressionsunary-expression:primary-expression+ unary-expression- unary-expression! unary-expression~ unary-expressionpre-increment-expressionpre-decrement-expressioncast-expression14.6.1 Unary plus operatorFor an operation of the form +x, unary operator overload resolution (§14.2.3) is applied to select a specificoperator implementation. The operand is converted to the parameter type of the selected operator, and thetype of the result is the return type of the operator. The predefined unary plus operators are:int operator +(int x);uint operator +(uint x);long operator +(long x);ulong operator +(ulong x);float operator +(float x);double operator +(double x);decimal operator +(decimal x);For each of these operators, the result is simply the value of the operand.153

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!