3

Here's an example of what I am doing now:

return
    shpc == 0 ? "Currently, based on your selection below, you have not yet identified any hidden cards in your card deck." :
    shpc == 1 ? "Currently, based on your selection below, you have one hidden card in your card deck." :
                $"Currently, based on your selection below, you have {shpc} hidden cards in your card deck. These will not be visible.";

The code words but not having much knowledge of what was added to switch I wonder if this could also be done with a switch expression?

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • 3
    you can replace this with a "classic" switch, no need for the new fancy features of c#8 switch – Gian Paolo Dec 28 '19 at 10:46
  • @GianPaolo the switch expression is far cleaner and easier to use than a `switch` statement with lots of `return`s or direct variable assignments in the case blocks. – Panagiotis Kanavos Dec 30 '19 at 09:35

2 Answers2

8

Try this

return shpc switch 
{
    0 => "Currently, based on your selection below, you have not yet identified any hidden cards in your card deck.",
    1 => "Currently, based on your selection below, you have one hidden card in your card deck.",
    _ => $"Currently, based on your selection below, you have {shpc} hidden cards in your card deck. These will not be visible."
};
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
3

Definitely:

return shpc switch 
{
    0 => "Currently, based on your selection below, you have not yet identified any hidden cards in your card deck.",
    1 => "Currently, based on your selection below, you have one hidden card in your card deck.",
    _ => $"Currently, based on your selection below, you have {shpc} hidden cards in your card deck. These will not be visible."
};


Alvin Sartor
  • 2,249
  • 4
  • 20
  • 36