c# switch expression examples

c# switch expression examples


C# Switch Expression Examples

Switch expressions and statements in C# provide a robust mechanism for controlling the flow of a program based on the values of expressions. They offer clear syntax and powerful functionality for handling multiple branching paths. This article delves into various uses and features of switch expressions and statements in C#.

C# Switch Expression Multiple Cases

Switch expressions introduced in C# 8.0 allow handling multiple cases in a single line. This makes the code not only cleaner but also easier to read and maintain.

 

var number = 3;
var result = number switch {
    1 or 2 => "One or Two",
    3 or 4 => "Three or Four",
    _ => "Other"
};

C# Switch Statement

The switch statement is a control statement that executes different parts of code based on the value of an expression. It is often used in place of long if-else if chains.

 

var day = 4;
switch (day) {
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    default:
        Console.WriteLine("Another day");
        break;
}

C# Switch Expression vs Statement

Switch expressions, a newer feature in C#, provide a more concise and functional way of handling multiple conditional branches, as compared to switch statements. They return values and reduce the boilerplate code seen with traditional switch statements.

C# Switch Expression Fall Through

Switch expressions do not allow fall through from one case label to another, which is a departure from traditional switch statements. This ensures each case is handled explicitly.

C# Switch on Type

Using type patterns in switch statements or expressions simplifies handling of different data types dynamically. This feature is especially useful in scenarios involving inheritance and interfaces.

object value = 10;
var typeResult = value switch {
    int i when i > 0 => "Positive integer",
    string s => "String",
    _ => "Unknown"
};

C# Switch Expression

The switch expression is used to directly return a value based on a single condition or a series of conditions.

 

var color = "red";
var hex = color switch {
    "red" => "#FF0000",
    "green" => "#00FF00",
    "blue" => "#0000FF",
    _ => "#FFFFFF"
};

C# Switch Case Multiple Conditions

Switch expressions and statements can evaluate multiple conditions using tuples, enhancing their utility in complex decision-making scenarios.

 

var state = "NY";
var city = "NYC";
var location = (state, city) switch {
    ("NY", "NYC") => "New York City",
    ("NY", _) => "Other part of New York",
    _ => "Outside New York"
};

C# Switch Statement String

Handling strings with switch statements is straightforward and allows for clear conditional logic based on string values.

 

var fruit = "apple";
switch (fruit) {
    case "apple":
        Console.WriteLine("Apple selected");
        break;
    case "banana":
        Console.WriteLine("Banana selected");
        break;
    default:
        Console.WriteLine("No fruit selected");
        break;
}

C# Switch New Syntax

The latest versions of C# have introduced enhancements in the syntax of switch expressions, making them more powerful and easier to use with pattern matching.

C# Switch Lambda

Switch expressions can be combined with lambda expressions to execute complex logic directly within the switch, facilitating cleaner and more modular code.

 

var response = 200;
var action = response switch {
    200 => () => Console.WriteLine("OK"),
    404 => () => Console.WriteLine("Not Found"),
    _ => () => Console.WriteLine("Unknown Status")
};
action();

Switch Case C# 10

C# 10 continues to refine the switch expression by expanding its capabilities with pattern matching, making the language more expressive and efficient for developers.

 

 

 

 

 


 

Leave a reply Your email address will not be published. Required fields are marked*

Popular Posts

Categories Clouds