Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda