How can I combine return
and switch case
statements?
I want something like
return switch(a)
{
case 1:\"lalala
If you want switch to return value, you can use delegate:
int a = 2;
string result = new Func(delegate ()
{
switch (a)
{
case 1: return "lalala";
case 2: return "blalbla";
case 3: return "lolollo";
default: return "default";
}
})();
Or:
int a = 2;
string result = new Func(delegate (int i)
{
switch (i)
{
case 1: return "lalala";
case 2: return "blalbla";
case 3: return "lolollo";
default: return "default";
}
})(a);
Or just use lambda:
int a = 2;
string result = new Func((int i) =>
{
switch (i)
{
case 1: return "lalala";
case 2: return "blalbla";
case 3: return "lolollo";
default: return "default";
}
})(a);