Is it possible to have a switch in C# which checks if the value is null or empty not "" but String.Empty
? I know i can do this:
switch (text)
{
case null:
case "":
break;
}
Is there something better, because I don't want to have a large list of IF statements?
I'mm trying to replace:
if (String.IsNullOrEmpty(text))
blah;
else if (text = "hi")
blah
I would suggest something like the following:
switch(text ?? String.Empty)
{
case "":
break;
case "hi":
break;
}
Is that what you are looking for?
What's wrong with your example switch
statement?
switch (text)
{
case null:
case "":
foo();
break;
case "hi":
bar();
break;
}
It works (and for some reason that surprised me - I thought it would complain or crash on the null
case) and it's clear.
For that matter, why are you worried about String.Empty
? I'm missing something here.
how about
if (string.isNullOrEmpty(text))
{
//blah
}
else
{
switch (text)
{
case "hi":
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With