Edit

Selection statements

Tip

This article is part of the Fundamentals section for developers who already know at least one programming language and are learning C#. If you're new to programming, start with the Get started tutorials first. For the complete syntax, see selection statements in the language reference.

Coming from another language? C++, Java, and JavaScript all share C's heritage, so if, else, and switch read the same in C#. The one difference worth noting: C# forbids fall-through between nonempty switch cases, and each case label can test a pattern rather than only a constant. If you're coming from Python, C#'s if/else if/else maps to if/elif/else, and C#'s pattern-based switch is closest to Python's match statement.

Selection statements choose which block of code runs based on a condition. C# provides two: if (with an optional else) for branching on a Boolean value, and switch for comparing one value against several cases. A Boolean expression is any expression that evaluates to true or false, such as the comparison temperature >= 25.

Branch with if and else

An if statement runs its block only when its Boolean expression is true. Add an else block to supply the code that runs when the condition is false:

int temperature = 28;

// An if statement runs its block only when the condition is true.
// The else block runs when the condition is false.
if (temperature >= 25)
{
    Console.WriteLine("Warm"); // => Warm
}
else
{
    Console.WriteLine("Cool");
}

The body of an if or else is a single statement, such as an assignment or a method call. A block statement is itself a single statement that encloses zero or more statements in braces ({ }).

Many coding standards recommend that you enclose the branch bodies in braces, even for a single statement. Braces make the scope explicit. It prevents a common mistake: adding a second line later that you expect to run conditionally, but that runs unconditionally instead. Only the braces decide which statements belong to the branch. C# doesn't treat whitespace as significant, so indentation alone never does. Braces are legal even around one line: the block is the single statement that the branch runs. Indent your code for readability, but rely on braces to define the block.

Test several conditions with else if

To choose among more than two paths, chain conditions with else if. C# evaluates each condition in order and runs the first block whose condition is true, then skips the rest. A final else handles every remaining case:

int score = 82;

// Chain conditions with else if. The first branch whose condition is
// true runs; the compiler skips the rest.
string grade;
if (score >= 90)
{
    grade = "A";
}
else if (score >= 80)
{
    grade = "B";
}
else if (score >= 70)
{
    grade = "C";
}
else
{
    grade = "F";
}

Console.WriteLine(grade); // => B

Order matters in a chain. Because the first matching condition wins, put the most specific conditions first. In the previous example, testing score >= 80 before score >= 70 ensures a score of 82 maps to "B" rather than "C".

Match a value with a switch statement

When you compare a single value against many discrete cases, a switch statement reads more clearly than a long else if chain. Like if, the switch statement is a branching statement: it chooses which code to run. Each case label lists a value to match, and each case section ends with a break statement. Stack labels to share one body, and use the default section for values that no case matches:

DayOfWeek day = DayOfWeek.Saturday;

// A switch statement compares one value against several case labels.
// Stacked labels share a body. Each section ends with break, and the
// default section runs when no case matches.
switch (day)
{
    case DayOfWeek.Saturday:
    case DayOfWeek.Sunday:
        Console.WriteLine("Weekend"); // => Weekend
        break;
    default:
        Console.WriteLine("Weekday");
        break;
}

Unlike C and Java, C# doesn't allow execution to fall through from one nonempty case section into the next. Every section that contains code must end with a break (or another jump statement, such as return or goto). This rule prevents the accidental fall-through bugs common in those languages.

Test patterns with case and when

A case label isn't limited to constant values. It can test a pattern, which is a rule that describes the shape or value of data. A relational pattern such as < 0 matches any value less than zero. Add a when clause to attach an extra condition that must also be true for the case to match:

int measurement = 42;

// A case label can test a pattern instead of a constant. A when clause
// adds a condition that must also be true for the case to match.
switch (measurement)
{
    case < 0:
        Console.WriteLine("Negative");
        break;
    case 0:
        Console.WriteLine("Zero");
        break;
    case > 0 when measurement % 2 == 0:
        Console.WriteLine("Positive and even"); // => Positive and even
        break;
    default:
        Console.WriteLine("Positive and odd");
        break;
}

Pattern-based cases are evaluated top to bottom, so more specific patterns belong before more general ones. For the full catalog of patterns, see pattern matching.

Select a value with an expression

The if and switch statements decide which code runs. When you instead need to choose a value, use an expression. An expression evaluates to a value, so you can use it anywhere a value is expected, such as the right side of an assignment or a method argument. C# offers expression forms that select a value from alternatives such as the conditional operator and the switch expression.

Conditional operator ?:

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

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

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 in the language reference.

switch expression

A switch expression is the expression counterpart to the switch statement. Instead of running code for the matching case, it evaluates to a value. It's more concise than assigning a value in each arm of a switch statement, and the compiler warns you when the arms don't cover every possible input. The switch expression is a core part of pattern matching. To learn when and how to use it, see pattern matching and the switch expression reference.

See also