Skip to content

Commit 9e3ac98

Browse files
authored
Revised conditional ref reference (#36361)
1 parent ab13696 commit 9e3ac98

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

docs/csharp/language-reference/operators/conditional-operator.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ The `condition` expression must evaluate to `true` or `false`. If `condition` ev
2626

2727
Beginning with C# 9.0, conditional expressions are target-typed. That is, if a target type of a conditional expression is known, the types of `consequent` and `alternative` must be implicitly convertible to the target type, as the following example shows:
2828

29-
[!code-csharp[target-typed conditional](snippets/shared/ConditionalOperator.cs#TargetTyped)]
29+
:::code language="csharp" source="snippets/shared/ConditionalOperator.cs" id="TargetTyped":::
3030

3131
If a target type of a conditional expression is unknown (for example, when you use the [`var`](../statements/declarations.md#implicitly-typed-local-variables) keyword) or the type of `consequent` and `alternative` must be the same or there must be an implicit conversion from one type to the other:
3232

33-
[!code-csharp[not target-typed conditional](snippets/shared/ConditionalOperator.cs#NotTargetTyped)]
33+
:::code language="csharp" source="snippets/shared/ConditionalOperator.cs" id="NotTargetTyped":::
3434

3535
The conditional operator is right-associative, that is, an expression of the form
3636

@@ -53,27 +53,27 @@ a ? b : (c ? d : e)
5353
5454
## Conditional ref expression
5555
56-
A [reference variable](../statements/declarations.md#reference-variables) can be assigned conditionally with a conditional ref expression. You can also use a conditional ref expression as a [reference return value](../keywords/ref.md#reference-return-values) or as a [`ref` method argument](../keywords/ref.md#passing-an-argument-by-reference).
56+
A conditional ref expression conditionally returns a variable reference, as the following example shows:
57+
58+
:::code language="csharp" interactive="try-dotnet-method" source="snippets/shared/ConditionalOperator.cs" id="ConditionalRef":::
59+
60+
You can [`ref` assign](assignment-operator.md#ref-assignment) the result of a conditional ref expression, use it as a [reference return](../statements/jump-statements.md#ref-returns) or pass it as a [`ref`](../keywords/ref.md#passing-an-argument-by-reference), [`out`](../keywords/out-parameter-modifier.md), or [`in`](../keywords/in-parameter-modifier.md) method parameter. You can also assign to the result of a conditional ref expression, as the preceding example shows.
5761
5862
The syntax for a conditional ref expression is as follows:
5963
6064
```csharp
6165
condition ? ref consequent : ref alternative
6266
```
6367
64-
Like the original conditional operator, a conditional ref expression evaluates only one of the two expressions: either `consequent` or `alternative`.
68+
Like the conditional operator, a conditional ref expression evaluates only one of the two expressions: either `consequent` or `alternative`.
6569

6670
In a conditional ref expression, the type of `consequent` and `alternative` must be the same. Conditional ref expressions aren't target-typed.
6771

68-
The following example demonstrates the usage of a conditional ref expression:
69-
70-
[!code-csharp-interactive[conditional ref](snippets/shared/ConditionalOperator.cs#ConditionalRef)]
71-
7272
## Conditional operator and an `if` statement
7373

7474
Use of the conditional operator instead of an [`if` statement](../statements/selection-statements.md#the-if-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:
7575

76-
[!code-csharp[conditional and if-else](snippets/shared/ConditionalOperator.cs#CompareWithIf)]
76+
:::code language="csharp" source="snippets/shared/ConditionalOperator.cs" id="CompareWithIf":::
7777

7878
## Operator overloadability
7979

0 commit comments

Comments
 (0)