Home
C#
case Examples
This page was last reviewed on Sep 24, 2024.
Dot Net Perls
Case. The C# keyword "case" 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.
switch
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.
when
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.
using System; int test = 5; string result = null; switch (test) { case 5: result = "Five"; break; default: result = "Not five"; break; } Console.WriteLine(result);
Five
Complex example. Here is an example that stacks cases. This code demonstrates the case keyword used in different ways. A string switch statement is shown.
String switch
Note The first 3 cases are stacked on top of each other. This syntax can match multiple cases to a single executable code block.
Note 2 At the end of each case statement block, you must have a break, return or go to jump statement for the program to compile.
break
return
goto
Important The default case does not use the "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
Case expressions. 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.
However The C# compiler detects unreachable code in switches with case expressions. Try adding a default here—it will be detected as unreachable.
Unreachable
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
Type cases. 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.
Part 1 We have an int local variable, and pass it as an argument to the Test method, which checks its type in a switch.
Part 2 We have a string, which is implicitly cast to an object. We pass this an argument to Test().
Part 3 We use a 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
Var keyword. 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.
var
Here We have a tuple called "data" and we match its 2 items and then reference those 2 items elsewhere in the code.
Info We use a 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
Constant expected. 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.
const
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 [...]
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 24, 2024 (new example).
Home
Changes
© 2007-2024 Sam Allen.