Home » C# Tutorial » C# Switch Case with Examples

C# Switch Case with Examples

If you are a beginner at C#, then from what you may already know about the language, it is a fairly simple coding script, with not too many frills. If you already on your way to mastering C#, then you know how every little component of your script, every tiniest function, offers immense potential, productivity, and utility. In this article, we will be looking at one such important tool in C#, which is the C# switch case or the C# switch statement as some would call it. Like most of the other tools in, C#, the switch case is a simple and easy to understand concept, that you will be able to master by the end of this article. Without further ado, let us dive right into it.

What is a switch case in C#?

Simply put, a C# switch statement is similar to an ‘if’ statement with a few changes. With the help of switch cases, we can check conditions with the utmost ease. To understand it further, we need to know that each switch statement is evaluated only once by the program.

The program then proceeds to compare the value associated with the switch case in C# with each ‘case’ that you include after the switch statement. The block of code that is associated with the statement is executed if the case matches the switch statement. Perhaps, a look at its base syntax will describe it more efficiently.

Syntax:

switch (expression)
{
case a:
// condition 1
break;
case b:
// condition 2
break;
default:
// default code block
break;
}

The ‘default’ and ‘break’ keywords will be explained further in this article. Let us proceed and examine some examples so that we understand how to master C# switch cases.

C# switch case example 1:

using system;
namespace switch case C#
{
class Program
{
static void Main(string[] args)
{
string name = “Pradtutorials”;
switch(name)
{
// the switch statement tells that program that if the name = Pradtutorials, then do this, if not, do that
case “Pradtutorials”:
Console.WriteLine(“That is the correct answer”);
break;
case “tutorial”:
Console.WriteLine(“Better luck next time”);
break;
default:
Console.WriteLine(“Are you even trying?”);
break;
}
}
}
}

OUTPUT:

That is the correct answer

Now, if we were to look at the flip side of the same example, wherein the following changes are made:

string name = “tutorial”;

Your output would be the following:

‘Better luck next time’

Break Statement in switch case C#:

What the break statement does is that it essentially prevents the execution from falling through to the next case statement. While working with C# switch cases, the ‘break’ keyword is a required component, without which you will be thrown an exception and will not be able to execute the program the way you want to. For similar conditional statements in other programming languages, the ‘break’ keyword is not necessary to run the program, it will just bleed the execution to the next case statement regardless of which case value is fulfilled. The C# switch statement is, therefore, more consistent due to the mandatory ‘break’ keyword.

Return Statement in C# switch cases:

If you were to add ‘return;’ after a break statement, you would essentially block the viewer’s access to the rest of the program’s execution. You can understand this with the following snippet from the earlier example.

case “tutorial”:
Console.WiriteLine(“Better luck next time”);
break;
Return;
// the return statement will not block the viewers’ access if the switch statement value matches the value of this particular case. If you add any more executable code after the switch function, it would only be accessible to the viewer if the switch condition is met.

Example 2:

using system;
namespace switch case C#
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetMonth(2));
Console.ReadLine();
}
//in this method, we will be converting numbers to months of the year using switch statements, wherein 0=January, 1=February, and so on
static string GetMonth(int monthNum){
string monthName;
switch (monthNum)
{
// the goal of this C# switch statement is to figure out what the ‘monthNum’ is and depending on which one it is, the program will execute a different output

case 0:
monthName = “January”;
break;
case 1:
monthName = “February”;
break;
case 2:
monthName = “March”;
break;
// you can continue adding how many ever cases you like
default:
monthName = “Invalid Month Number”;
break;
}
return monthName;
}

OUTPUT:

March

As seen in the previous example, the program looks for the name of the month ‘monthName’ associated to the number ‘2’ entered in “Console.WriteLine(GetMonth(2));” and then executes the program.

However, if the viewer were to enter a number that was not previously established within the code, they will not be able to avail any value for ‘monthName.’ This is where the Default statement comes into play.

In case a viewer inserts a value that is not acceptable or established within the code already, the value that you assign to your default statement will be the output to said viewer. In the previous example, if a viewer were to give an input of ‘47’, their output would be ‘Invalid Month Number

Example 3:

In this final demonstration, let us look at how we optimize the C# switch cases to let us execute the C# program with much less syntax than that of the ‘if’ statement. For instance, let us look at academic grades and we can use switch statements to make C# work for us

using system;
namespace switch case C#
{
class Program
{
static void Main()
{
int grade;
Console.Write(“Enter grade:”);
grade = Convert.ToInt32 (Console.ReadLine());
switch (grade)
{
case 1:
Console.WriteLine (“Excellent”);
break;
case 2:
Console.WriteLine (“Good”);
break;
case 3:
Console.WriteLine (“Fair”);
break;
default:
Console.WriteLine(“Ivalid Grade”);
break;
}
// Note that the data type of the constant should match the data type of the variable being evaluated by the switch construct
}
}
}

OUTPUT:

Enter grade:

As you may have already guessed, 

  • If the viewer enters 1, the output will be ‘Excellent’
  • If the viewer enters 2, the output will be ‘Good’
  • If the viewer enters 3, the output will be ‘Fair’
  • If the viewer enters any other number aside form 1,2 and 3, the output will be ‘Invalid Grade

Conclusion:

Everything that you can do with C# switch statements, you can do the same with ‘If’ statements. However, the reason that we have switch statements is that they make the same process much easier and quicker in terms of preparation and execution. You could possibly do the same with ‘if’ statements, but it will be much messier and harder to manage than switch cases in C#

Unlike the ‘if’ statement, the switch case is extremely helpful if there are numerous variables that need to be matched, as it saves time, effort, and resources to do so. As always, you can use the switch case C# in various other methods to execute a number of different results. As it is so versatile and easy to understand, you will find that the use of switch statements is made in all kinds of programs to automate various functions, no matter how simple or complex they may be. Thus, with practice, you too can master C# switch statements.

Do you know?
1. C# Enum
2. C# Dictionary
3. C# Switch Case
4. C# Substring
5. C# Foreach

Pin It on Pinterest