switch (expression)
{
case constant1:
// code block
break;
case constant2:
// code block
break;
default:
// default code block
break;
}
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Another day");
break;
}
The switch statement evaluates the expression once and compares its result to each case label. If a match is found, the corresponding block of code runs until a break statement is encountered or the switch statement ends.
The break keyword is used to terminate a case in the switch block. Without break, the program continues to the next case (known as "fall-through" behavior).
The default keyword is used when none of the case values match the expression. It is optional but highly recommended for catching unanticipated input.
The switch statement supports the following data types:
string command = "start";
switch (command)
{
case "start":
Console.WriteLine("System starting");
break;
case "stop":
Console.WriteLine("System stopping");
break;
default:
Console.WriteLine("Unknown command");
break;
}
enum Day { Monday, Tuesday, Wednesday }
Day today = Day.Tuesday;
switch (today)
{
case Day.Monday:
Console.WriteLine("Start of the week");
break;
case Day.Tuesday:
Console.WriteLine("Second day");
break;
default:
Console.WriteLine("Midweek or later");
break;
}
int level = 2;
string role = "admin";
switch (level)
{
case 1:
Console.WriteLine("Beginner");
break;
case 2:
switch (role)
{
case "admin":
Console.WriteLine("Admin at Level 2");
break;
case "user":
Console.WriteLine("User at Level 2");
break;
}
break;
}
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;
}
string weather = "sunny";
string message = weather switch
{
"sunny" => "Wear sunglasses",
"rainy" => "Take an umbrella",
_ => "Check the forecast"
};
Console.WriteLine(message);
if-else chains| Aspect | If-Else | Switch |
|---|---|---|
| Condition Type | Boolean Expressions | Equality Comparisons |
| Data Type | Any | int, char, string, enum |
| Readability | Poor with many branches | Better for multiple options |
| Performance | Similar | Similar |
string input = Console.ReadLine();
switch (input)
{
case "help":
Console.WriteLine("Showing help...");
break;
case "exit":
Console.WriteLine("Exiting program...");
break;
default:
Console.WriteLine("Unknown command");
break;
}
char operation = '+';
int a = 10, b = 5;
switch (operation)
{
case '+':
Console.WriteLine(a + b);
break;
case '-':
Console.WriteLine(a - b);
break;
case '*':
Console.WriteLine(a * b);
break;
case '/':
Console.WriteLine(a / b);
break;
default:
Console.WriteLine("Invalid operation");
break;
}
The switch statement in C# is a robust tool for handling multiple conditional paths based on the value of a single expression. With support for traditional syntax, enums, strings, and modern features like pattern matching and switch expressions, it's a highly versatile structure for clear and efficient decision-making. Mastering its use will significantly enhance your code's readability and maintainability, especially in scenarios involving multiple conditions.
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