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.6 Unary expressions
14.6.6 Cast expressions
Paragraph 11 A cast-expression is used to explicitly convert an expression to a given type. cast-expression : ( type ) unary-expression
Paragraph 21 A cast-expression of the form (T)E, where T is a type and E is a unary-expression, performs an explicit conversion (§13.2) of the value of E to type T. 2 If no explicit conversion exists from the type of E to T, a compile-time error occurs. 3 Otherwise, the result is the value produced by the explicit conversion. 4 The result is always classified as a value, even if E denotes a variable.
Paragraph 31 The grammar for a cast-expression leads to certain syntactic ambiguities. 2 For example, the expression (x)-y could either be interpreted as a cast-expression (a cast of -y to type x) or as an additive-expression combined with a parenthesized-expression (which computes the value x -y).
Paragraph 41 To resolve cast-expression ambiguities, the following rule exists: A sequence of one or more tokens (§9.4) enclosed in parentheses is considered the start of a cast-expression only if at least one of the following are true: - 2 The sequence of tokens is correct grammar for a type, but not for an expression.
- 3 The sequence of tokens is correct grammar for a type, and the token immediately following the closing parentheses is the token "~", the token "!", the token "(", an identifier (§9.4.1), a literal (§9.4.4), or any keyword (§9.4.3) except as and is.
[Note: The above rule means that only if the construct is unambiguously a cast-expression is it considered a cast-expression. end note]
Paragraph 51 The term "correct grammar" above means only that the sequence of tokens must conform to the particular grammatical production. 2 It specifically does not consider the actual meaning of any constituent identifiers. 3 For example, if x and y are identifiers, then x.y is correct grammar for a type, even if x.y doesn't actually denote a type. [Note: From the disambiguation rule, it follows that, if x and y are identifiers, (x)y, (x)(y), and (x)(-y) are cast-expressions, but (x)-y is not, even if x identifies a type. However, if x is a keyword that identifies a predefined type (such as int), then all four forms are cast-expressions (because such a keyword could not possibly be an expression by itself). end note] |
Jagger Software Ltd |
Company # 4070126 |
VAT # 762 5213 42 |

