Multiple cases in c# 8.0 switch expressions

Multiple cases in c# 8.0 switch expressions

C# 8.0 introduced switch expressions, which allow you to use a more concise syntax for switch statements. With switch expressions, you can also handle multiple cases using the when keyword.

Here's an example of how to use multiple cases in C# 8.0 switch expressions:

int number = 5; string result = number switch { 1 or 2 => "One or two", >= 3 and <= 6 => "Three to six", _ => "Other" }; 

In this example, we use the or keyword to handle multiple cases for the values 1 and 2. We also use the when keyword to check if the value is between 3 and 6.

The when keyword allows you to add additional conditions to a case. If the condition is true, the code inside the case will be executed. If the condition is false, the code inside the case will be skipped.

The _ is used as a catch-all case to handle all other values.

Examples

  1. How to handle multiple cases in C# 8.0 switch expressions?

    • Description: In C# 8.0, you can handle multiple cases in a switch expression by separating them with a comma.
    int value = 3; string result = value switch { 1 => "One", 2 => "Two", 3 or 4 => "Three or Four", _ => "Other" }; 
  2. C# 8.0 switch expressions with multiple conditions

    • Description: C# 8.0 allows using multiple conditions separated by 'or' in switch expressions for concise and readable code.
    char ch = 'A'; string result = ch switch { 'A' or 'E' or 'I' or 'O' or 'U' => "Vowel", _ => "Consonant" }; 
  3. Using ranges with multiple cases in C# 8.0 switch expressions

    • Description: C# 8.0 introduces ranges in switch expressions, enabling concise handling of multiple cases.
    int value = 10; string result = value switch { < 0 => "Negative", >= 0 and <= 10 => "Between 0 and 10", > 10 => "Greater than 10", _ => "Other" }; 
  4. C# 8.0 switch expressions with multiple enum cases

    • Description: Enumerations in C# 8.0 switch expressions can have multiple cases specified using 'or'.
    enum Day { Monday, Tuesday, Wednesday, Thursday, Friday } Day today = Day.Friday; string result = today switch { Day.Monday or Day.Tuesday or Day.Wednesday or Day.Thursday or Day.Friday => "Weekday", Day.Saturday or Day.Sunday => "Weekend", _ => "Unknown" }; 
  5. Handling null and multiple cases in C# 8.0 switch expressions

    • Description: C# 8.0 switch expressions support handling null along with multiple cases for more expressive code.
    string str = null; string result = str switch { null => "Null", "foo" or "bar" => "Matched 'foo' or 'bar'", _ => "Other" }; 
  6. C# 8.0 switch expressions with multiple patterns

    • Description: C# 8.0 switch expressions support patterns, allowing for more flexible and powerful matching.
    object obj = new Random(); string result = obj switch { int i or double d => $"Numeric value: {obj}", string s => $"String value: {obj}", _ => "Other" }; 
  7. C# 8.0 switch expressions with multiple types

    • Description: You can use multiple types in C# 8.0 switch expressions for handling various scenarios.
    object obj = 10; string result = obj switch { int or double => $"Numeric value: {obj}", string => $"String value: {obj}", _ => "Other" }; 
  8. C# 8.0 switch expressions with multiple complex cases

    • Description: C# 8.0 switch expressions can handle multiple complex cases effectively, improving code readability.
    (int x, int y) = (3, 4); string result = (x, y) switch { (0, 0) => "Origin", (0, _) => "Y-axis", (_, 0) => "X-axis", (_, _) => "Other" }; 
  9. C# 8.0 switch expressions with multiple type patterns

    • Description: Type patterns in C# 8.0 switch expressions enable handling multiple types succinctly.
    object obj = "Hello"; string result = obj switch { string or int => $"String or int: {obj}", double => $"Double: {obj}", _ => "Other" }; 
  10. Nested switch expressions with multiple cases in C# 8.0

    • Description: Nested switch expressions in C# 8.0 can handle multiple cases at different levels, providing flexible control flow.
    char ch = 'A'; string result = ch switch { 'A' => "Capital A", 'B' => "Capital B", _ => ch switch { 'a' => "Lowercase a", 'b' => "Lowercase b", _ => "Other" } }; 

More Tags

mongoid darknet php-7.3 intel-mkl media sitemap linear-interpolation magnific-popup pull-to-refresh bloburls

More C# Questions

More Pregnancy Calculators

More Animal pregnancy Calculators

More Biology Calculators

More Stoichiometry Calculators