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

139
Views
Si o cambiar? ¿Cuál de estos es un código más limpio y convencional?

Aprendí que si puede usar cajas de interruptores, debe usarlas, porque tienen un mejor rendimiento. ¿Cuál de los siguientes fragmentos de código sería más convencional? ¿Y cuántos casos que se comportan exactamente igual siguen estando bien para las sentencias switch y cuándo debería empezar a usar las sentencias if?

 Console.Write("Insert a card value: "); int value = Int32.Parse(Console.ReadLine()); string symbol = ""; switch(value) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 9: case 10: symbol = Convert.ToString(value); break; case 11: symbol = "J"; break; case 12: symbol = "Q"; break; case 13: symbol = "K"; break; case 14: symbol = "A"; break; } Console.WriteLine("The " + symbol + " has a value of " + value);

¿O tal vez este?

 Console.Write("Insert a card value: "); int value = Int32.Parse(Console.ReadLine()); string symbol = ""; if (value <= 10) symbol = Convert.ToString(value); else if (value == 11) symbol = "J"; else if (value == 12) symbol = "Q"; else if (value == 13) symbol = "K"; else if (value == 14) symbol = "A"; Console.WriteLine("The " + symbol + " has a value of " + value);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

A partir de C#9, hay una nueva coincidencia de patrones que hace que el cambio sea muy legible:

 string symbol = value switch { >=1 and <=10 => Convert.ToString(value), 11 => "J", 12 => "Q", 13 => "K", 14 => "A", _ => "" };

Demostración en línea: https://dotnetfiddle.net/tovo7d

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!