The Switch Statement in C#? is a versatile control structure that allows developers to execute one block of code among many options based on the value of a single variable. It simplifies complex conditional logic and enhances code readability, especially when dealing with multiple values or scenarios.
The Switch Statement in C#? is used to test the value of a variable against a list of predefined constants. Each constant is defined in a case block, and the matching case is executed. If no match is found, an optional default block is executed.
switch (expression) { case value1: // code to execute break; case value2: // code to execute break; default: // code to execute if no case matches break; }
The break keyword prevents fall-through to the next case. The default case is optional but useful as a fallback.
The switch expression is evaluated once. The result is compared with each case label. If a match is found, that case's block is executed until a break statement is reached. If no case matches, the default block is executed.
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("Other number"); break; } } }
In this example, the output will be "Two" because the number variable holds the value 2.
string fruit = "apple"; switch (fruit) { case "apple": Console.WriteLine("Apples are red."); break; case "banana": Console.WriteLine("Bananas are yellow."); break; default: Console.WriteLine("Unknown fruit."); break; }
enum Season { Spring, Summer, Autumn, Winter } Season current = Season.Summer; switch (current) { case Season.Spring: Console.WriteLine("It's springtime."); break; case Season.Summer: Console.WriteLine("It's summertime."); break; default: Console.WriteLine("Other season."); break; }
object value = 100; switch (value) { case int i: Console.WriteLine($"Integer: {i}"); break; case string s: Console.WriteLine($"String: {s}"); break; default: Console.WriteLine("Unknown type"); break; }
This feature allows type checking and extraction within the switch statement using modern C# syntax.
int score = 85; string grade = score switch { >= 90 => "A", >= 80 => "B", >= 70 => "C", >= 60 => "D", _ => "F" }; Console.WriteLine($"Grade: {grade}");
Switch expressions are concise and powerful, allowing conditional logic to be assigned directly to variables.
Consider using the Switch Statement in C#? when:
The Switch Statement in C#? is an essential control structure that simplifies decision-making in code. Whether you are evaluating strings, integers, or enums, the switch statement provides a readable and organized way to handle multiple conditions.
With the introduction of pattern matching and switch expressions, modern C# has made the switch statement even more powerful and expressive.
If you want clean, efficient branching logic, especially when dealing with discrete values, then mastering the Switch Statement in C#? is a must.
Copyrights © 2024 letsupdateskills All rights reserved