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.

<strong>C#</strong> LANGUAGE SPECIFICATION• Parenthesized sub-expressions, which are themselves constant expressions.• Cast expressions, provided the target type is one of the types listed above.• The predefined +, –, !, and ~ unary operators.• The predefined +, –, *, /, %, , &, |, ^, &&, ||, ==, !=, , = binary operators, providedeach operand is of a type listed above.• The ?: conditional operator.Whenever an expression is of one of the types listed above and contains only the constructs listed above, theexpression is evaluated at compile-time. This is true even if the expression is a sub-expression of a largerexpression that contains non-constant constructs.The compile-time evaluation of constant expressions uses the same rules as run-time evaluation of nonconstantexpressions, except that where run-time evaluation would have thrown an exception, compile-timeevaluation causes a compile-time error to occur.Unless a constant expression is explicitly placed in an unchecked context, overflows that occur in integraltypearithmetic operations and conversions during the compile-time evaluation of the expression alwayscause compile-time errors (§14.5.12).Constant expressions occur in the contexts listed below. In these contexts, a compile-time error occurs if anexpression cannot be fully evaluated at compile-time.• Constant declarations (§17.3).• Enumeration member declarations (§21.30).• case labels of a switch statement (§15.7.2).• goto case statements (§15.9.3).• Dimension lengths in an array creation expression (§14.5.10.2) that includes an initializer.• Attributes (§24).An implicit constant expression conversion (§13.1.6) permits a constant expression of type int to beconverted to sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant expressionis within the range of the destination type.14.16 Boolean expressionsA boolean-expression is an expression that yields a result of type bool.boolean-expression:expressionThe controlling conditional expression of an if-statement (§15.7.1), while-statement (§15.8.1), do-statement(§15.8.2), or for-statement (§15.8.3) is a boolean-expression. The controlling conditional expression of the?: operator (§14.12) follows the same rules as a boolean-expression, but for reasons of operator precedenceis classified as a conditional-or-expression.A boolean-expression is required to be of a type that can be implicitly converted to bool or of a type thatimplements operator true. [Note: As required by §17.9.1, any type that implements operator truemust also implement operator false. end note] If neither requirement is satisfied, a compile-time erroroccurs.When a boolean expression is of a type that cannot be implicitly converted to bool but does implementoperator true, then following evaluation of the expression, the operator true implementationprovided by that type is invoked to produce a bool value.[Note: The DBBool struct type in §18.4.2 provides an example of a type that implements operator trueand operator false. end note]176

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

Saved successfully!

Ooh no, something went wrong!