C# Case Example

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

C# Case Example

You are interested in specifics about the case keyword and the syntax for using cases in switch
statements in the C# programming language. The case keyword provides a way to specify a
constant to be matched in the switch selection statement, and cases can be stacked and combined.
Here we look closely at the case keyword in the C# language and see how it can be applied in a
switch statement to control the flow of the execution engine.

Using case keyword


First, the case keyword in the C# programming language is specific to the switch statement and
is a way to specify constants that can match the selection in the switch statement. The code
blocks following a specific case statement are only executed when the case constants are
matched. This example demonstrates the case keyword used in different ways.

--- Program that uses case statements in switch (C#) ---

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 of the program ---

Multiple of ten
Multiple of ten
Multiple of fifty
*
Invalid

Using several case statements together. The program text shows the TestCase method and
internally this method uses several case statements in a switch statement. The first three cases are
stacked on top of each other and this is a valid syntax for matching multiple cases to a single
executable code block. At the end of each case statement block, you must have a break, return or
goto jump statement for the program to compile correctly.

See Goto Switch Usage.

Omitting parentheses in case blocks. The program text also shows how it is possible for you to
omit the surrounding parenthesis in a case block in the switch statement. This option is useful for
very large or deeply nested switches, or switch statements that have many very short blocks.
There is no difference in the compiled program if you omit the parentheses.

Using constant identifier instead of constant directly. The switch statement in the TestCase
method also shows how you can 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 text also 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.

Case fall through


Here we note how the C# programming 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, as shown in the example program text. This feature was
added to the language to reduce the rate of switch statement fall through programming mistakes.

Summary
Here we looked at the case keyword in the C# programming language and explored its usage in
a simple example program. The case statement in a switch statement is specified with a constant,
which can be either directly embedded in the statement or the identifier of a constant defined
elsewhere. We saw how you can combine or stack case statements. We also compared the case
keyword to the default statement in a switch construct.

See Keywords.

See Switch Overview.

© 2007-2011 Sam Allen. All rights reserved.

You might also like