Usage. Cases can be stacked and combined. We can target a case with a goto statement. And "default" is a special kind of case—it is matched when nothing else does.
Simple example. Cases specify constants that match the selection in a switch statement. The blocks following a specific case statement are only executed when the case constants are matched.
Info Default matches all values that are not matched by the specified case statements. It is like "else" in an if-else chain.
static void Main()
int test = 5;
string result = null;
result = "Five";
result = "Not five";
Complex example. Here is a more complex example. This code demonstrates the case keyword used in different ways. A string switch statement is shown.
Detail The default case does not use the "case" keyword. It is the case that is matched when no other cases do.
static string TestCase(string value)
const string _special = "constant";
// Begin the switch.switch (value)
// You can use the parentheses in a case body.
return "Multiple of ten";
// You can omit the parentheses and stack the cases.
return "Multiple of fifty";
// You can use a constant identifier in the case.
// You can use the default case.
static void Main()
// Test the method.
}Multiple of ten
Multiple of ten
Multiple of fifty
Constant expected. It is important to know what values are constant before using them in a switch. For example, string.Empty is not constant—it is a field.
So We cannot use string.Empty in a case statement. Other fields in a class also cannot be cases.
Detail The C# compiler will report the error to us before the program is ever run. This is helpful.
static void Main(string args)
A constant value is expected
Syntax tip. It is possible to omit the surrounding parentheses in a case block. This option is useful for large or deeply nested switches, or switch statements that have many short blocks.
And There is no difference in the compiled program if you omit the parentheses.
Also The switch statement also shows how to use a constant field or local constant in the case statements.
Detail The C# compiler will internally treat this constant the same way as explicitly specified constants in other cases.