You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The conditional operator (`?:`), commonly known as the ternary conditional operator, returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.
15
+
The conditional operator `?:`, commonly known as the ternary conditional operator, evaluates a Boolean expression, and returns the result of evaluating one of two expressions, depending on whether the Boolean expression evaluates to `true` or `false`. Beginning with C# 7.2, the [conditional ref expression](#conditional-ref-expression) returns the reference to the result of one of the two expressions.
16
+
17
+
The syntax for the conditional operator is as follows:
17
18
18
19
```csharp
19
-
condition?first_expression:second_expression;
20
+
condition?consequence:alternative
20
21
```
21
22
22
-
Beginning with C# 7.2, the `first_expression`and `second_expression` my be [`ref` expressions](https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/conditional-ref.md):
23
+
The `condition` expression must evaluate to `true` or `false`. If `condition` evaluates to `true`, the `consequence` expression is evaluated, and its result becomes the result of the operation. If `condition` evaluates to `false`, the `alternative` expression is evaluated, and its result becomes the result of the operation. Only `consequence` or `alternative` is evaluated.
The type of `consequence` and `alternative` must be the same, or there must be an implicit conversion from one type to the other.
27
26
28
-
The result may be assigned to a `ref` or `ref readonly` variable, or to a variable with neither modifier.
27
+
The conditional operator is right-associative, that is, an expression of the form
29
28
30
-
## Remarks
29
+
```csharp
30
+
a?b:c?d:e
31
+
```
31
32
32
-
The `condition` must evaluate to `true` or `false`. If `condition`is `true`, `first_expression` is evaluated and becomes the result. If `condition` is `false`, `second_expression` is evaluated and becomes the result. Only one of the two expressions is evaluated. This is particularly important for expressions where the result is a `ref`, as the following is valid:
When the result is a value, the type of `first_expression` and `second_expression` must be the same, or there must be an implicit conversion from one type to the other. When the result is a `ref`, the type of `first_expression` and `second_expression` must be the same.
43
+
## Conditional ref expression
41
44
42
-
You can express calculations that might otherwise require an `if-else` construction more concisely by using the conditional operator. For example, the following code uses first an `if` statement and then a conditional operator to classify an integer as positive or negative.
45
+
Beginning with C# 7.2, you can use the conditional ref expression to return the reference to the result of one of the two expressions. You can assign that reference to a [local ref](../keywords/ref.md#ref-locals) or [local ref readonly](../keywords/ref.md#ref-readonly-locals) variable, or use it as a [reference return value](../keywords/ref.md#reference-return-values) or as a [`ref` method parameter](../keywords/ref.md#passing-an-argument-by-reference).
46
+
47
+
The syntax for the conditional ref expression is as follows:
43
48
44
49
```csharp
45
-
intinput=Convert.ToInt32(Console.ReadLine());
46
-
stringclassify;
47
-
48
-
// if-else construction.
49
-
if (input>0)
50
-
classify="positive";
51
-
else
52
-
classify="negative";
53
-
54
-
// ?: conditional operator.
55
-
classify= (input>0) ?"positive":"negative";
50
+
condition?refconsequence:refalternative
56
51
```
57
52
58
-
The conditional operator is right-associative. The expression `a ? b : c ? d : e` is evaluated as `a ? b : (c ? d : e)`, not as `(a ? b : c) ? d : e`.
59
-
60
-
The conditional operator cannot be overloaded.
61
-
62
-
## Example
53
+
Like the original conditional operator, the conditional ref expression evaluates only one of the two expressions: either `consequence` or `alternative`.
63
54
64
-
The following example shows the conditional operator whose result is a value:
55
+
In the case of the conditional ref expression, the type of `consequence` and `alternative` must be the same.
For more information, see the [feature proposal note](https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/conditional-ref.md).
62
+
63
+
## Conditional operator and an `if..else` statement
64
+
65
+
Use of the conditional operator over an [if-else](../keywords/if-else.md) statement might result in more concise code in cases when you need conditionally to compute a value. The following example demonstrates two ways to classify an integer as negative or nonnegative:
66
+
67
+
[!code-csharp[conditional and if-else](~/samples/snippets/csharp/language-reference/operators/ConditionalExamples.cs#CompareWithIf)]
68
+
69
+
## Operator overloadability
70
+
71
+
The conditional operator cannot be overloaded.
67
72
68
-
The following alternative shows the conditional operator where the result is a reference:
For more information, see the [Conditional operator](~/_csharplang/spec/expressions.md#conditional-operator) section of the [C# language specification](../language-specification/index.md).
0 commit comments