C# Switch Statement

In this article I am going to discuss about the C# Switch Statement in detail but before that if you had not read my previous article of C# Else-If Statement then must read it. In C# Programming we had studied if-else statements and also studied else-if statement and when to use this statements but is this always a good approach? The answer is no. In C# we use else-if statement only if we have maximum 4-5 conditions but if we have more than 5 conditions then it is not a good practice to use else-if statements then for solving that problem switch statement comes into existence.
Introduction to C# Switch Statement
In C# Switch Statement is one of the important selection statement in the control flow statements. Switch statement is one of the best alternative for if else-if statement. In C#, the Switch statement is a multiway branch statement. It provides an very good way to transfer the execution to different piece of code based on value of the expression. The switch expression is of any type which includes integer, character, strings and enums. In switch statement the expression is checked for different cases and the match case will be executed.
Why we need Switch Statement?
As I already told, Switch Statements are one of the alternative to avoid long if else-if statements. In C#, switch is a keyword(reserved word) and using this keyword we can create selection statements for multiple blocks and each block is constructed using case keyword. So when we have multiple options and we only have to choose one condition among others then we need to go for switch statement because as per selected option it executes the piece of code. The syntax for switch statement is given below:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default://when any of the case(condition) not met then this default block is // executed like the else block in if-else statement.
// code block
break;
}
C# Program to Explain Switch Statement
using System;
namespace ControlFlowDemo
{
class Program
{
static void Main(string[] args)
{
int number = 2;
switch (number)
{
case 1: //here we check if case is equal to number
Console.WriteLine("Number is 1");
break;
case 2:
Console.WriteLine("Number is 2");
break;
case 3:
Console.WriteLine("Number is 3");
break;
default:
Console.WriteLine("Number other than 1, 2 and 3");
break;
}
Console.ReadKey();
}
}
}
Output:
Number is 2
In above code you have noticed that we have used break keyword which is type of jump statement. We have break statement to come out of the switch statement. If we don’t use break statement then it will also check the next cases after getting result. Therefore for stopping the switch statement from checking next cases after getting result we have to use break statement.
Is it Mandatory to use Default Block?
No, Default Block is not necessary in switch statement. As I already told default block acts like else block of if-else statement. In switch statement when any of the condition not met then default block is executed but if we have not put the default block in our switch statement then no block will be executed inside switch statement.
Switch Statement with Enums in C#
We can also use C# Switch Statement with enums to check for the cases. For implementing Switch statement with enums first we have to make one enum then we have to pass the enum to switch keyword and then we have to check for enum cases. If you don’t know about enum then don’t worry we will discuss the enums in detail in our next articles. But for now you can understand that enum is user defined datatype that is used to storing any type of data. Let see an example to understand switch statement with enums.
public enum Color //creating enum of color
{
Red,
Green,
Blue,
}
using System;
namespace SwitchStatementWithEnums
{
class Program
{
static void Main(string[] args)
{
Color name=Color.Red; //how to access enum value
switch (name)
{
case Color.Red:
Console.WriteLine("Color is Red");
break;
case Color.Blue:
Console.WriteLine("Color is Blue");
break;
case Color.Green:
Console.WriteLine("Color is Green");
break;
}
Console.ReadKey();
}
}
}
For reading more about switch statement like nested switch statements and how we can use this you can go through the given link.
Subscribe to my newsletter
Read articles from WebTutorialStack directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

WebTutorialStack
WebTutorialStack
I am a web developer with great experience in dot net frameworks and also a competitive programmer.