C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
We use this keyword to match constant values in switches. This keyword specifies a constant to be matched in the switch selection statement. Cases can be stacked and combined.
Example. Case is specific to the switch statement. It is a way to specify constants that match the selection in the switch statement. The code blocks following a specific case statement are only executed when the case constants are matched.
Next: This example demonstrates the case keyword used in different ways. A string switch statement is shown.
Default: The default case does not use the "case" keyword. It is the case that is matched when no other cases do.
Based on: .NET 4 C# program that uses case statements in switch 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)); } } Output Multiple of ten Multiple of ten Multiple of fifty * Invalid
The program shows the TestCase method, which uses several case statements in a switch statement. The first three cases are stacked on top of each other. This is a valid syntax for matching multiple cases to a single executable code block.
Note: At the end of each case statement block, you must have a break, return or goto jump statement for the program to compile.
It is possible to omit the surrounding parenthesis in a case block in the switch statement. 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.
The switch statement also shows how to use a constant field or local constant in the case statements. The C# compiler will internally treat this constant the same way as explicitly specified constants in other cases.
Default case. The program shows the default case statement in the switch statement. The keyword default actually matches all values that are not matched by the specified case statements.
Fall through. The C# language does not allow cases to fall through to the next cases. This means you cannot execute separate code blocks in multiple case statements. You can still "stack" together multiple cases.
Note: This feature was added to the language to reduce the rate of switch statement fall through programming mistakes.
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 is embedded in the statement or the identifier of a constant defined elsewhere.