C# 8 switch expression with multiple cases with same result

C# 8 switch expression with multiple cases with same result

Starting with C# 8, you can use the switch expression to simplify multiple cases with the same result by using the "when" keyword. Here is an example:

 string color = "blue"; string result = color switch { "red" => "The color is red", "green" => "The color is green", "blue" => "The color is blue", _ when color.StartsWith("#") => "The color is a hex value", _ => "The color is not red, green, or blue" }; 

In this example, the first three cases match the string value of color, and the fourth case uses the when keyword to check if the value of color starts with "#". The _ is used as a placeholder for any other values that don't match the previous cases.

If color equals "blue" in this example, the result will be "The color is blue". If color equals "#FF0000", the result will be "The color is a hex value". If color equals "yellow", the result will be "The color is not red, green, or blue".

Examples

  1. "C# 8 Switch Expression Multiple Cases Same Result"

    • Code Implementation:
      var result = option switch { 1 or 2 => "One or Two", 3 => "Three", _ => "Other", }; 
    • Description: Use the or pattern to have multiple cases with the same result in a C# 8 switch expression.
  2. "C# 8 Switch Expression Duplicate Cases"

    • Code Implementation:
      var result = day switch { DayOfWeek.Monday or DayOfWeek.Friday => "Start or end of the week", DayOfWeek.Wednesday => "Midweek", _ => "Other", }; 
    • Description: Demonstrate the use of or to create duplicate cases with the same result in a switch expression.
  3. "C# 8 Switch Expression Multiple Values Same Output"

    • Code Implementation:
      var result = input switch { "Yes" or "Y" => "Affirmative", "No" or "N" => "Negative", _ => "Other", }; 
    • Description: Combine multiple values with the or pattern to produce the same output in a switch expression.
  4. "C# 8 Switch Expression Grouped Cases"

    • Code Implementation:
      var result = code switch { "200" or "201" or "202" => "Successful", "404" or "403" => "Client Error", _ => "Other", }; 
    • Description: Group cases with the same result using the or pattern in a C# 8 switch expression.
  5. "C# 8 Switch Expression Same Result for Ranges"

    • Code Implementation:
      var result = score switch { >= 90 and <= 100 => "A", >= 80 and < 90 => "B", _ => "Other", }; 
    • Description: Apply the and pattern to have the same result for multiple ranges in a switch expression.
  6. "C# 8 Switch Expression Same Output for Multiple Types"

    • Code Implementation:
      var result = value switch { int or long or double => "Numeric", string => "String", _ => "Other", }; 
    • Description: Use the or pattern to provide the same output for multiple types in a switch expression.
  7. "C# 8 Switch Expression Same Result for Multiple Enum Values"

    • Code Implementation:
      var result = status switch { Status.Pending or Status.Processing => "In Progress", Status.Completed => "Finished", _ => "Other", }; 
    • Description: Use the or pattern to map multiple enum values to the same result in a switch expression.
  8. "C# 8 Switch Expression Same Output for Days"

    • Code Implementation:
      var result = day switch { DayOfWeek.Monday or DayOfWeek.Wednesday or DayOfWeek.Friday => "Workday", DayOfWeek.Saturday or DayOfWeek.Sunday => "Weekend", _ => "Other", }; 
    • Description: Group days with the same result using the or pattern in a switch expression.
  9. "C# 8 Switch Expression Same Result for Multiple Conditions"

    • Code Implementation:
      var result = input switch { "Yes" or "Y" and IsValid => "Valid Affirmative", "No" or "N" and IsValid => "Valid Negative", _ => "Other", }; 
    • Description: Apply the or and and patterns to have the same result for multiple conditions in a switch expression.
  10. "C# 8 Switch Expression Same Output for File Extensions"

    • Code Implementation:
      var result = extension switch { ".txt" or ".md" or ".docx" => "Text Document", ".jpg" or ".png" => "Image", _ => "Other", }; 
    • Description: Use the or pattern to assign the same output for multiple file extensions in a switch expression.

More Tags

rtts extjs multi-select spring-boot splunk-query firebase-console distcp microservices crash gradle-tooling-api

More C# Questions

More Livestock Calculators

More Auto Calculators

More Geometry Calculators

More Organic chemistry Calculators