Skip to content

Commit 3339f1b

Browse files
pkulikovRon Petrusha
authored andcommitted
Revised the conditional operator article (#9125)
* Revised the conditional operator article * Addressed feedback
1 parent b911e0d commit 3339f1b

File tree

1 file changed

+48
-42
lines changed

1 file changed

+48
-42
lines changed
Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "?: Operator (C# Reference)"
3-
ms.date: "07/20/2015"
3+
ms.date: "11/20/2018"
44
f1_keywords:
55
- "?:_CSharpKeyword"
66
- "?_CSharpKeyword"
@@ -10,70 +10,76 @@ helpviewer_keywords:
1010
- "conditional operator (?:) [C#]"
1111
ms.assetid: e83a17f1-7500-48ba-8bee-2fbc4c847af4
1212
---
13-
1413
# ?: Operator (C# Reference)
1514

16-
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:
1718

1819
```csharp
19-
condition ? first_expression : second_expression;
20+
condition ? consequence : alternative
2021
```
2122

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.
2324

24-
```csharp
25-
ref condition ? ref first_expression : ref second_expression;
26-
```
25+
The type of `consequence` and `alternative` must be the same, or there must be an implicit conversion from one type to the other.
2726

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
2928

30-
## Remarks
29+
```csharp
30+
a ? b : c ? d : e
31+
```
3132

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:
33+
is evaluated as
3334

3435
```csharp
35-
ref (storage != null) ? ref storage[3] : ref defaultValue;
36+
a ? b : (c ? d : e)
3637
```
3738

38-
The reference to `storage` is not evaluated when `storage` is null.
39+
The following example demonstrates the usage of the conditional operator:
40+
41+
[!code-csharp[non ref condtional](~/samples/snippets/csharp/language-reference/operators/ConditionalExamples.cs#ConditionalValue)]
3942

40-
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
4144

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:
4348

4449
```csharp
45-
int input = Convert.ToInt32(Console.ReadLine());
46-
string classify;
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 ? ref consequence : ref alternative
5651
```
5752

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`.
6354

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.
6556

66-
[!code-csharp[csRefOperators?:](~/samples/snippets/csharp/language-reference/operators/ConditionalExamples.cs#ConditionalValue)]
57+
The following example demonstrates the usage of the conditional ref expression:
58+
59+
[!code-csharp[conditional ref](~/samples/snippets/csharp/language-reference/operators/ConditionalExamples.cs#ConditionalRef)]
60+
61+
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.
6772

68-
The following alternative shows the conditional operator where the result is a reference:
73+
## C# language specification
6974

70-
[!code-csharp[csRefOperatorsRef?:](~/samples/snippets/csharp/language-reference/operators/ConditionalExamples.cs#ConditionalRef)]
75+
For more information, see the [Conditional operator](~/_csharplang/spec/expressions.md#conditional-operator) section of the [C# language specification](../language-specification/index.md).
7176

72-
## See Also
77+
## See also
7378

74-
- [C# Reference](../../../csharp/language-reference/index.md)
75-
- [C# Programming Guide](../../../csharp/programming-guide/index.md)
76-
- [C# Operators](../../../csharp/language-reference/operators/index.md)
77-
- [if-else](../../../csharp/language-reference/keywords/if-else.md)
78-
- [?. and ?[] Operators](../../../csharp/language-reference/operators/null-conditional-operators.md)
79-
- [?? Operator](../../../csharp/language-reference/operators/null-coalescing-operator.md)
79+
- [C# Reference](../index.md)
80+
- [C# Programming Guide](../../programming-guide/index.md)
81+
- [C# Operators](index.md)
82+
- [if-else statement](../keywords/if-else.md)
83+
- [?. and ?[] Operators](null-conditional-operators.md)
84+
- [?? Operator](null-coalescing-operator.md)
85+
- [ref keyword](../keywords/ref.md)

0 commit comments

Comments
 (0)