switch Statement

noun

say it: “SWICH STAYT-ment” /swɪtʃ ˈsteɪtmənt/

A C# statement that compares one value with a list of cases and runs the code for the one that matches.

What is switch Statement in C#?

switch Statement — A statement that compares one value against a list of cases and runs the one that matches, or default when none does. In C# a case can't fall through into the next: each one ends with break or return.

string size = "M";
switch (size)
{
    case "S": Console.WriteLine("Small"); break;
    case "M": Console.WriteLine("Medium"); break;
    default: Console.WriteLine("Large"); break;
}
// Medium

In a sentence

  • “I replaced six else-ifs on the shirt size with one switch statement.”

Level: Apprentice · More C# terms at this level

Updated

In other subjects