C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Caution: Not every variable can be used in a switch. Most built-in value types, like int or char, work.
Strings: A switch can handle strings. These are implemented in a different way. They use a hidden Dictionary.
C# program that uses switch
using System;
class Program
{
static void Main()
{
int value = 5;
switch (value)
{
case 1:
Console.WriteLine(1);
break;
case 5:
Console.WriteLine(5);
break;
}
}
}
Output
5
Here: We see how the curly brackets are used in the switch cases. And we combine some of the case statements.
C# program that uses int switch
using System;
class Program
{
static void Main()
{
while (true)
{
Console.WriteLine("Type number and press Return");
try
{
int i = int.Parse(Console.ReadLine());
switch (i)
{
case 0:
case 1:
case 2:
{
Console.WriteLine("Low number");
break;
}
case 3:
case 4:
case 5:
{
Console.WriteLine("Medium number");
break;
}
default:
{
Console.WriteLine("Other number");
break;
}
}
}
catch
{
}
}
}
}
Output
Type number and press Return
5
Medium number
Type number and press Return
2
Low number
Type number and press Return
500
Other number
Note: The C# compiler detects a string switch and can optimize it with a Dictionary lookup.
Note 2: Small string switches, like this one with just 3 cases, are often not compiled into Dictionaries. Performance is better this way.
C# program that switches on string
using System;
class Program
{
static void Main()
{
string value = "turnip";
// ... Switch on the string.
switch (value)
{
case "lettuce":
Console.WriteLine("LETTUCE");
break;
case "squash":
Console.WriteLine("SQUASH");
break;
case "turnip":
Console.WriteLine("TURNIP");
break;
}
}
}
Output
TURNIP
TestNameAndCode: This method switches on the code int, and then tests the name string. Goto and break are used to redirect control flow.
Case: We use this keyword to specify a matching constant. Default does not use this keyword. We end a case with break, return or continue.
CaseBreak: Break and continue can be used in any switch statement. These 2 keywords are used also within loops—this can be confusing.
BreakContinueQuestion: Are you breaking out of a switch, or out of the enclosing loop? Scope is important. The deepest construct is broken first.
C# program that uses case, goto, default in switch
using System;
class Program
{
static void TestNameAndCode(string name, int code)
{
switch (code)
{
case 200:
case 300:
case 400:
if (name == "bird")
{
Console.WriteLine("Bird 200, 300, or 400");
break;
}
goto default;
default:
Console.WriteLine("Unknown");
break;
}
}
static void Main()
{
// These will enter the if-statement.
TestNameAndCode("bird", 200);
TestNameAndCode("bird", 400);
// This will go to the default case.
TestNameAndCode("cat", 400);
}
}
Output
Bird 200, 300, or 400
Bird 200, 300, or 400
Unknown
But: Our code logic, with nested switches, quickly turns into a mess. With comments, this approach may succeed.
Here: I test the first two elements in an int array with switches. The second element is tested if the first is 4.
C# program that uses nested switch
using System;
class Program
{
static void Main()
{
int[] array = { 4, 10, 14 };
switch (array[0])
{
case 3:
Console.WriteLine(3); // Not reached.
break;
case 4:
Console.WriteLine(4);
// ... Use nested switch.
switch (array[1])
{
case 10:
Console.WriteLine(10);
break;
}
break;
}
}
}
Output
4
10
Here: We introduce a class hierarchy—the Bird and Cat classes inherit from Animal. We then create some class instances.
And: We match the types of the Animal class. The most derived class is matched first—in this switch form, order matters.
C# program that uses switch, matches types
using System;
class Animal
{
public int size;
}
class Bird : Animal
{
public int color;
}
class Cat : Animal
{
public bool wild;
}
class Program
{
static void Test(Animal animal)
{
// Switch on a class type with pattern matching.
switch (animal)
{
case Cat c:
Console.WriteLine($"CAT wild = {c.wild}");
break;
case Bird b:
Console.WriteLine($"BIRD color = {b.color}");
break;
case Animal a:
Console.WriteLine($"ANIMAL size = {a.size}");
break;
}
}
static void Main()
{
// Create some class instances.
Cat cat = new Cat();
cat.wild = true;
Bird bird = new Bird();
bird.color = 5;
Animal animal = new Animal();
animal.size = 10;
// Test class instances.
Test(cat);
Test(bird);
Test(animal);
}
}
Output
CAT wild = True
BIRD color = 5
ANIMAL size = 10
Tip: We have repeat "case 200" statements. They are different only because of their "when" clauses.
Tip 2: With the "when pattern-matching" syntax, order matters in a switch. This is an enhanced syntax form.
C# program that uses pattern matching, when keyword
using System;
class Program
{
static void Main()
{
int value = 200;
int secondValue = 300;
// Use switch with pattern matching.
switch (value)
{
case 200 when secondValue == 0:
Console.WriteLine("Y");
break;
case 200 when secondValue == 300:
Console.WriteLine("Value is 200, secondValue is 300");
break;
case 400:
Console.WriteLine("Z");
break;
}
}
}
Output
Value is 200, secondValue is 300
Bool: A bool may be used in the expression of a switch. But this is somewhat strange as only true and false are allowed.
Nullable: A nullable type can be used, but only if the nullable type "wraps" a valid switch type like an enum.
C# program that causes switch double error
class Program
{
static void Main()
{
double value = 1.4;
switch (value)
{
case 1:
break;
}
}
}
Output
Error 1
A switch expression or case label must be a bool, char, string,
integral, enum, or corresponding nullable type....
Goto: We can use the goto statement, as in "goto case 1," to run both cases on a 0 value. As shown, the program does not compile.
C# program that has fall-through error
using System;
class Program
{
static void Main()
{
int value = 0;
// ... Every switch statement must be terminated.
switch (value)
{
case 0:
Console.WriteLine("Zero");
case 1:
Console.WriteLine("One");
break;
}
}
}
Output
Error 1
Control cannot fall through from one case label ('case 0:') to another
C# program that has duplicate case
using System;
class Program
{
static void Main()
{
short number = 0;
// ... Cases may not be duplicated.
switch (number)
{
case 0:
case 0:
Console.WriteLine("ZERO");
return;
case 1:
Console.WriteLine("ONE");
return;
}
}
}
Output
Error 1
The label 'case 0:' already occurs in this switch statement
C# program that lacks constant case
using System;
class Program
{
static void Main()
{
int number = 0;
int test = 10;
// ... Constant values are required.
switch (number)
{
case test + 1:
Console.WriteLine(100);
return;
case 0:
Console.WriteLine(0);
return;
}
}
}
Output
Error 1
A constant value is expected
Version 1: This version of the code uses a switch statement. It returns an int based on the argument int "v."
Version 2: This code does the same thing as Method 1, but instead uses a series of if-statements.
Result: The benchmark shows that the switch statement version is slightly faster. Those nanoseconds may come in handy someday.
C# program that benchmarks switch
using System;
using System.Diagnostics;
class Program
{
static int Method1(int v)
{
switch (v)
{
case 0:
return 10;
case 1:
return -1;
case 2:
return 20;
default:
return 0;
}
}
static int Method2(int v)
{
if (v == 0) return 10;
if (v == 1) return -1;
if (v == 2) return 20;
return 0;
}
static void Main()
{
Method1(0); Method2(0);
const int max = 100000000;
var s1 = Stopwatch.StartNew();
// Version 1: use switch.
for (int i = 0; i < max; i++)
{
Method1(0);
Method1(1);
Method1(2);
Method1(3);
}
s1.Stop();
var s2 = Stopwatch.StartNew();
// Version 2: use if-else.
for (int i = 0; i < max; i++)
{
Method2(0);
Method2(1);
Method2(2);
Method2(3);
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
max).ToString("0.00 ns"));
Console.Read();
}
}
Output
9.25 ns [switch]
9.85 ns [if]
1. If and switch. The if-statement sometimes performs better than the switch. Testing is essential.
If, Switch Comparison2. Regex and switch. The switch statement can be used to replace a Regex method call. This can make programs much faster.
Regex, Switch3. Intermediate language: Switch is often implemented at a lower level with the switch opcode. We show an example.
IL: switchSo: If you want to use switches everywhere in your code, go ahead. But don't expect to be admired for it.
Quote: The problem with switch statements is essentially that of duplication. Often you find the same switch statement scattered around a program in different places. If you add a new clause to the switch, you have to find all these switch statements and change them (Refactoring).