Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

?:: practical reference

?:: practical reference: ?: selects exactly one of two expressions according to a bool condition.

Version: C# language; verified against C# 14 reference

Syntax

condition ? consequent : alternative

?: selects exactly one of two expressions according to a bool condition.

Use it for a short value choice, not for elaborate side effects.

Only the chosen branch runs; target typing and conversions determine the result type.

Nesting is right-associative and needs parentheses for readability.

The complete program actually uses ?:; its exact expected stdout is pass.

Examples

using System;
using System.Linq;
using System.Threading.Tasks;

var score = 8; Console.WriteLine(score >= 5 ? "pass" : "fail");

Result

pass

The complete program actually uses `?:`; its exact expected stdout is `pass`.

Sources