Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/csharp/fundamentals/statements/selection.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Selection statements in C#"
description: Use if, else, and switch statements to choose which code runs based on a condition, including pattern-based case labels and when clauses.
ms.date: 07/09/2026
ms.date: 08/20/2026
ms.topic: concept-article
ai-usage: ai-assisted
---
Expand Down Expand Up @@ -55,11 +55,11 @@ The `if` and `switch` statements decide which code runs. When you instead need t

### Conditional operator `?:`

The conditional operator `?:` chooses between two values based on a Boolean condition. It takes the form `condition ? valueIfTrue : valueIfFalse`:
The conditional operator `?:` chooses one of two values based on a Boolean condition.

:::code language="csharp" source="./snippets/selection-statements/Program.cs" id="Ternary":::

Use `?:` when you assign one of two values, because it keeps the assignment in one place and lets you mark the variable `readonly` or `const`. Prefer an `if` statement when the branches do more than produce a value. For the operator's precedence and associativity rules, see the [conditional operator](../../language-reference/operators/conditional-operator.md) in the language reference.
<!-- Remove these comment markers after dotnet/docs#55469 merges and this branch is rebased.
For its syntax, short-circuit behavior, and guidance on choosing it instead of `if`/`else`, see [Conditional operator `?:`](../expressions/operators.md#conditional-operator-).
-->
Comment on lines +58 to +62

### `switch` expression

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public static void Main()
ElseIfExample();
SwitchStatementExample();
SwitchWhenExample();
TernaryExample();
}

private static void IfElseExample()
Expand Down Expand Up @@ -104,16 +103,4 @@ private static void SwitchWhenExample()
// </SwitchWhen>
}

private static void TernaryExample()
{
// <Ternary>
int hour = 9;

// The conditional operator ?: chooses between two values in a single
// expression: condition ? valueIfTrue : valueIfFalse.
string greeting = hour < 12 ? "Good morning" : "Good afternoon";

Console.WriteLine(greeting); // => Good morning
// </Ternary>
}
}
Loading