You are wondering if you should use the switch statement to test enums or ints in the C# programming language. Find out if it results in clearer code, and whether it is faster. Here we examine how you can switch on ints and enums in the C# language, with examples.
This example shows how you can use enums in switches in the C# language. Enums are useful in your program if it has to use magic constants for whatever reason. For the switch statement, look at the IsImportant method defined at the bottom of this example.
=== Example program that switches on int (C#) ===
using System;
enum Priority
{
Zero,
Low,
Medium,
Important,
Critical
};
class Program
{
static void Main()
{
// New local variable of the Priority enum type.
Priority priority = Priority.Zero;
// Set priority to critical on Monday.
if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
{
priority = Priority.Critical;
}
// Write this if the priority is important.
if (IsImportant(priority))
{
Console.WriteLine("The problem is important.");
}
// See if Low priority is important.
priority = Priority.Low;
Console.WriteLine(IsImportant(priority));
// See if Important priority is.
priority = Priority.Important;
Console.WriteLine(IsImportant(priority));
}
static bool IsImportant(Priority priority)
{
// Switch on the Priority enum.
switch (priority)
{
case Priority.Low:
case Priority.Medium:
case Priority.Zero:
default:
return false;
case Priority.Important:
case Priority.Critical:
return true;
}
}
}
=== Output of the program ===
(First line is only written on Monday.)
The problem is important.
False
TrueDescription of the example program. This program defines a custom method that contains a switch, which tests the parameter that is an enum of type Priority. It returns true if the Priority value is Important or Critical; otherwise it returns false. You can use the switch here as a kind of filter for enum ranges.
(See Bool Methods, Returning True and False.)
How the enum switch is compiled. The above C# code is compiled into a special .NET framework instruction called a jump table. The jump table uses the IL instruction switch. It defines one jump "point" to each case. The actual instruction is this: "L_0004: switch (L_001f, L_001f, L_001f, L_0023, L_0023)".
For completeness, we see here how you can implement a switch statement using ints. The program accepts an int from the user and then tests it for six different values. You see how the curly brackets { } can be used in the switch cases, and how you can combine the case statements.
class Program
{
static void Main()
{
while (true)
{
System.Console.WriteLine("Type number and press Return");
try
{
int i = int.Parse(System.Console.ReadLine());
switch (i)
{
case 0:
case 1:
case 2:
{
System.Console.WriteLine("Low number");
break;
}
case 3:
case 4:
case 5:
{
System.Console.WriteLine("Medium number");
break;
}
default:
{
System.Console.WriteLine("Other number");
break;
}
}
}
catch
{
}
}
}
}How does the switch statement perform? You will find that it is often faster than an if-statement, but not always. To examine this issue in more detail, you can look at the separate article on this site.
(See If Versus Switch Performance.)
Here we saw examples of how you can switch on ints and enums in the C# language, and also pointed at a benchmark. Switch can improve performance when it is implemented by a jump table in the MSIL. When you have a set of constants such as the ones shown in this article, the author recommends you use switch, as it can have better performance, and the code is clearer to read. You can find more details on the if-statement, which can always replace a switch statement.