How can I combine return
and switch case
statements?
I want something like
return switch(a)
{
case 1:\"lalala
switch
and return
can't combine that way, because switch
is a statement, not an expression (i.e., it doesn't return a value).
If you really want to use just a single return
, you could make a Dictionary to map the switch variable to return values:
var map = new Dictionary()
{
{1, "lala"},
{2, "lolo"},
{3, "haha"},
};
string output;
return map.TryGetValue(a, out output) ? output : "default";