Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specification


14 Expressions
14.15 Constant expressions
Paragraph 11 A constant-expression is an expression that can be fully evaluated at compile-time. constant-expression : expression
Paragraph 21 The type of a constant expression can be one of the following: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, any enumeration type, or the null type. 2 The following constructs are permitted in constant expressions: - 3 Literals (including the null literal).
- 4 References to const members of class and struct types.
- 5 References to members of enumeration types.
- 6 Parenthesized sub-expressions, which are themselves constant expressions.
- 7 Cast expressions, provided the target type is one of the types listed above.
- 8 The predefined +, -, !, and ~ unary operators.
- 9 The predefined +, -, *, /, %, <<, >>, &, |, ^, &&, ||, ==, !=, <, >, <=, and >= binary operators, provided each operand is of a type listed above.
- 10 The ?: conditional operator.
Paragraph 31 Whenever an expression is of one of the types listed above and contains only the constructs listed above, the expression is evaluated at compile-time. 2 This is true even if the expression is a sub-expression of a larger expression that contains non-constant constructs.
Paragraph 41 The compile-time evaluation of constant expressions uses the same rules as run-time evaluation of non-constant expressions, except that where run-time evaluation would have thrown an exception, compile-time evaluation causes a compile-time error to occur.
Paragraph 51 Unless a constant expression is explicitly placed in an unchecked context, overflows that occur in integral-type arithmetic operations and conversions during the compile-time evaluation of the expression always cause compile-time errors (§14.5.12).
Paragraph 61 Constant expressions occur in the contexts listed below. 2 In these contexts, a compile-time error occurs if an expression cannot be fully evaluated at compile-time. - 3 Constant declarations (§17.3).
- 4 Enumeration member declarations (§21.30).
- 5 case labels of a switch statement (§15.7.2).
- 6 goto case statements (§15.9.3).
- 7 Dimension lengths in an array creation expression (§14.5.10.2) that includes an initializer.
- 8 Attributes (§24).
Paragraph 71 An implicit constant expression conversion (§13.1.6) permits a constant expression of type int to be converted to sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant expression is within the range of the destination type. |
Jagger Software Ltd |
Company # 4070126 |
VAT # 762 5213 42 |

