13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14 Expressions[Example: The exampleusing System;class Test{static void Main() {Type[] t = {typeof(int),typeof(System.Int32),typeof(string),typeof(double[]),typeof(void) };for (int i = 0; i < t.Length; i++) {Console.WriteLine(t[i].FullName);}}}produces the following output:System.Int32System.Int32System.StringSystem.Double[]System.VoidNote that int and System.Int32 are the same type. end example]14.5.12 The checked and unchecked operatorsThe checked and unchecked operators are used to control the overflow checking context for integral-typearithmetic operations and conversions.checked-expression:checked ( expression )unchecked-expression:unchecked ( expression )The checked operator evaluates the contained expression in a checked context, and the uncheckedoperator evaluates the contained expression in an unchecked context. A checked-expression or uncheckedexpressioncorresponds exactly to a parenthesized-expression (§14.5.3), except that the contained expressionis evaluated in the given overflow checking context.The overflow checking context can also be controlled through the checked and unchecked statements(§15.11).The following operations are affected by the overflow checking context established by the checked andunchecked operators and statements:• The predefined ++ and -- unary operators (§14.5.9 and §14.6.5), when the operand is of an integraltype.• The predefined - unary operator (§14.6.2), when the operand is of an integral type.• The predefined +, -, *, and / binary operators (§14.7), when both operands are of integral types.• Explicit numeric conversions (§13.2.1) from one integral type to another integral type, or from float ordouble to an integral type.When one of the above operations produce a result that is too large to represent in the destination type, thecontext in which the operation is performed controls the resulting behavior:• In a checked context, if the operation is a constant expression (§14.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 thedestination type.151

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

Saved successfully!

Ooh no, something went wrong!