The C# Switch Keyword is one of the most important decision-making statements in C# programming. In modern C# development, especially while working with .NET applications, ASP.NET Core, console applications, and enterprise software systems, the switch statement plays a critical role in controlling program flow efficiently.
If you are learning C# programming language, understanding how the switch statement in C# works is essential for writing clean, optimized, and maintainable code. The switch keyword allows developers to execute different blocks of code based on the value of a variable or expression.
The switch keyword in C# is a selection statement that allows a variable to be tested against multiple possible values. Each value is called a case, and the variable being switched on is checked for each case.
When a match is found, the corresponding block of code executes.
The switch statement is mainly used when:
Below is the basic syntax of the C# switch statement:
switch(expression)
{
case value1:
// Code block
break;
case value2:
// Code block
break;
case value3:
// Code block
break;
default:
// Default code block
break;
}
Letβs see a simple example of switch in C#:
using System;
class Program
{
static void Main()
{
int number = 2;
switch(number)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
case 3:
Console.WriteLine("Three");
break;
default:
Console.WriteLine("Invalid Number");
break;
}
}
}
Output:
Two
When the switch statement executes:
In many cases, the C# compiler optimizes switch statements using jump tables for better performance compared to multiple if-else statements.
Switch improves readability and maintainability in structured decision-making scenarios.
The break statement is mandatory in C# switch blocks to prevent fall-through.
case 1:
Console.WriteLine("Hello");
break;
Without break, C# will throw a compilation error (unless explicitly using fall-through with empty cases).
C# allows multiple case labels for the same code block:
switch(day)
{
case "Saturday":
case "Sunday":
Console.WriteLine("Weekend");
break;
default:
Console.WriteLine("Weekday");
break;
}
C# supports switching on string values:
string fruit = "Apple";
switch(fruit)
{
case "Apple":
Console.WriteLine("Red Fruit");
break;
case "Banana":
Console.WriteLine("Yellow Fruit");
break;
default:
Console.WriteLine("Unknown Fruit");
break;
}
Switch works very effectively with enums.
enum Days
{
Monday,
Tuesday,
Wednesday
}
class Program
{
static void Main()
{
Days today = Days.Monday;
switch(today)
{
case Days.Monday:
Console.WriteLine("Start of Week");
break;
case Days.Tuesday:
Console.WriteLine("Second Day");
break;
default:
Console.WriteLine("Mid Week");
break;
}
}
}
Switch can be nested inside another switch:
switch(category)
{
case 1:
switch(subCategory)
{
case 1:
Console.WriteLine("Subcategory 1");
break;
}
break;
}
Introduced in modern C# versions, the switch expression provides a more concise syntax.
int number = 3;
string result = number switch
{
1 => "One",
2 => "Two",
3 => "Three",
_ => "Invalid"
};
Console.WriteLine(result);
Switch expressions improve readability and reduce boilerplate code.
Modern C# supports pattern matching inside switch statements.
object obj = 10;
switch(obj)
{
case int i:
Console.WriteLine("Integer: " + i);
break;
case string s:
Console.WriteLine("String: " + s);
break;
default:
Console.WriteLine("Unknown Type");
break;
}
Switch statements are generally faster than long if-else chains when:
However, performance difference is negligible in small applications.
The C# Switch Keyword is a powerful and essential control statement in C# programming. Whether you are building console applications, web applications, or enterprise systems, mastering the switch statement in C# will significantly improve your coding efficiency and readability.
With the introduction of switch expressions and pattern matching in C#, modern C# has made decision-making logic more powerful and expressive than ever before.
C# is primarily used on the Windows .NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved