The C# case
-keyword is part of switch
. We use this keyword to match constant values in switches. Case specifies a constant to be matched in the switch
selection statement.
We can target a case with a goto
-statement. And "default" is a special kind of case—it is matched when nothing else does.
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.
case
-statements. It is like "else" in an if-else
chain.using System; int test = 5; string result = null; switch (test) { case 5: result = "Five"; break; default: result = "Not five"; break; } Console.WriteLine(result);Five
Here is an example that stacks cases. This code demonstrates the case keyword used in different ways. A string
switch
statement is shown.
case
-statement block, you must have a break
, return or goto
statement for the program to compile.case
-keyword. It is the case that is matched when no other cases are matched.using System; class Program { static string TestCase(string value) { const string _special = "constant"; // Begin the switch. switch (value) { case "100": case "1000": case "10000": { // You can use the parentheses in a case body. return "Multiple of ten"; } case "500": case "5000": case "50000": // You can omit the parentheses and stack the cases. return "Multiple of fifty"; case _special: // You can use a constant identifier in the case. return "*"; default: // You can use the default case. return "Invalid"; } } static void Main() { // Test the method. Console.WriteLine(TestCase("100")); Console.WriteLine(TestCase("1000")); Console.WriteLine(TestCase("5000")); Console.WriteLine(TestCase("constant")); Console.WriteLine(TestCase(null)); } }Multiple of ten Multiple of ten Multiple of fifty * Invalid
We can use case with expressions in newer versions of the C# language. These are compiled in a similar way to the equivalent if
-statements.
using System; int z = 999; // Test the value with complex case statements. switch (z) { case > 2000: Console.WriteLine("> 2000"); break; case < 2000 and not 999: Console.WriteLine("< 2000 and not 999"); break; case > 100: Console.WriteLine("> 100"); break; }> 100
We can test the type of an object with a switch
-statement. We use switch
on an object (or other type) and then specify possible derived types in cases.
int
local variable, and pass it as an argument to the Test method, which checks its type in a switch
.string
, which is implicitly cast to an object. We pass this an argument to Test()
.switch
-statement on the object argument, and each case is a possible derived type of the object.using System; class Program { public static void Main() { // Part 1: use int as argument. int a = 999; object z = a; Test(z); // Part 2: use string as argument. object z2 = "x"; Test(z2); } static void Test(object z) { // Part 3: check types in case statements. switch (z) { case int: Console.WriteLine("Type is int"); break; case string: Console.WriteLine("Type is string"); break; } } }Type is int Type is string
It is possible to capture local variables from part of a match in a case
-statement. We use the var
keyword, with a "case var
" statement.
when
-clause to test the second item in the tuple, and print out the value of the first item.using System; var data = (1000, 'x'); // Match the tuple with case var. switch (data) { case var (id, c) when c == 'x': Console.WriteLine($"Second item is x, {id}"); break; case var (id, _): Console.WriteLine($"Second item is not x, {id}"); break; }Second item is x, 1000
It is important to know what values are constant before using them in a switch
. We cannot have a case which is a local variable, as it is not a constant.
int value = 100; int value2 = 100; switch (value) { case value2: // This won't compile because the case is not constant. break; }error CS9135: A constant value of type 'int' is expected [...]
Case is used in switch
-statements. We also find this keyword in certain goto
-statements. The case
-statement is specified with a constant, which may be defined elsewhere.