Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
C# switch case one line with fallthrough

Recently as of C# 8.0 they introduced a new switch case that is rapidly replacing our nested ternary if statements in our code base as such:

var number = 1;
var evenness = number switch 
{ 
    1 => "odd", 
    2 => "even", 
    3 => "odd", 
    4 => "even",
    _=>"out of range" 
};

And I freaking love it! It's really nice, clean and succinct.

However, this could be improved with the following old style switch statement from this question Multiple cases in switch statement

string eventness2;
switch (number)
{
    case 1: case 3:
        eventness2 = "odd";
        break;
    case 2: case 4:
        eventness2 = "event";
        break;
    default:
        eventness2 = "out of range";
        break;
}

My question is, is there any way to support multiple cases within a one line switch statement similar to this invalid syntax

var evenness = number switch { 1,3 => "odd", 2,4 => "even",_=>"out of range";
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Of course, using pattern matching:

var evenness = number switch { 1 or 3 => "odd", 2 or 4 => "even", _ => "out of range" };
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!