12 Conditional operator
The ?: operator is called the conditional operator. It is at times also called the ternary operator.
conditional-expression:
conditional-or-expression
conditional-or-expression ? expression : expression
A conditional expression of the form b? x: y first evaluates the condition b. Then, if b is true, x is evaluated and becomes the result of the operation. Otherwise, y is evaluated and becomes the result of the operation. A conditional expression never evaluates both x and y.
The conditional operator is right-associative, meaning that operations are grouped from right to left. For example, an expression of the form a? b: c? d: e is evaluated as a? b: (c? d: e).
The first operand of the ?: operator must be an expression of a type that can be implicitly converted to bool, or an expression of a type that implements operator true. If neither of these requirements are satisfied, a compile-time error occurs.
The second and third operands of the ?: operator control the type of the conditional expression. Let X and Y be the types of the second and third operands. Then,
• If X and Y are the same type, then this is the type of the conditional expression.
• Otherwise, if an implicit conversion (§6.1) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.
• Otherwise, if an implicit conversion (§6.1) exists from Y to X, but not from X to Y, then X is the type of the conditional expression.
• Otherwise, no expression type can be determined, and a compile-time error occurs.
The run-time processing of a conditional expression of the form b? x: y consists of the following steps:
• First, b is evaluated, and the bool value of b is determined:
• If an implicit conversion from the type of b to bool exists, then this implicit conversion is performed to produce a bool value.
• Otherwise, the operator true defined by the type of b is invoked to produce a bool value.
• If the bool value produced by the step above is true, then x is evaluated and converted to the type of the conditional expression, and this becomes the result of the conditional expression.
• Otherwise, y is evaluated and converted to the type of the conditional expression, and this becomes the result of the conditional expression.
7.13 Assignment operators
The assignment operators assign a new value to a variable, a property, or an indexer element.
assignment:
unary-expression assignment-operator expression
Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
127
C# LANGUAGE REFERENCE
assignment-operator: one of
= += -= *= /= %= &= |= ^= <<= >>=
The left operand of an assignment must be an expression classified as a variable, a property access, or an indexer access.
The = operator is called the simple assignment operator. It assigns the value of the right operand to the variable, property, or indexer element given by the left operand. The simple assignment operator is described in §7.13.1.
The operators formed by prefixing a binary operator with an = character are called the compound assignment operators. These operators perform the indicated operation on the two operands, and then assign the resulting value to the variable, property, or indexer element given by the left operand. The compound assignment operators are described in §7.13.2.
The assignment operators are right-associative, meaning that operations are grouped from right to left. For example, an expression of the form a = b = c is evaluated as a = (b = c).
7.13.1 Simple assignment
The = operator is called the simple assignment operator. In a simple assignment, the right operand must be an expression of a type that is implicitly convertible to the type of the left operand. The operation assigns the value of the right operand to the variable, property, or indexer element given by the left operand.
The result of a simple assignment expression is the value assigned to the left operand. The result has the same type as the left operand and is always classified as a value.
If the left operand is a property or indexer access, the property or indexer must have a set accessor. If this is not the case, a compile-time error occurs.
The run-time processing of a simple assignment of the form x = y consists of the following steps:
• If x is classified as a variable:
• x is evaluated to produce the variable.
• y is evaluated and, if required, converted to the type of x through an implicit conversion (§6.1).
• If the variable given by x is an array element of a reference-type, a run-time check is performed to ensure that the value computed for y is compatible with the array instance of which x is an element. The check succeeds if y is null, or if an implicit reference conversion (§6.1.4) exists from the actual type of the instance referenced by y to the actual element type of the array instance containing x. Otherwise, an ArrayTypeMismatchException is thrown.
• The value resulting from the evaluation and conversion of y is stored into the location given by the evaluation of x.
• If x is classified as a property or indexer access: