views:

1797

answers:

7

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.

im trying to replace:

if (String.IsNullOrEmpty(text))
    blah;
else if (text = "hi")
    blah
A: 

For strings, I think you could do:

if (object.Equals(text,"text"))
{
}

MSDN docs.

tvanfosson
+4  A: 

how about

if (string.isNullOrEmpty(text))
{
   //blah
}
else
{
 switch (text)
 {
     case "hi":
 }

}

JoshBerke
The question was to avoid if-else statements.
James Pogran
If you look at the edit history the original question didn't include anything about if else;-)
JoshBerke
Also this avoids the style of if clauses he was worried about by only have a single if else as opposed to if elseif elseif etc..
JoshBerke
+11  A: 

I would suggest something like the following:

switch(text ?? String.Empty)
{
    case "":
        break;
    case "hi":
        break;
}

Is that what you are looking for?

Maxim
+3  A: 

From the documentation of String.Empty:

The value of this field is the zero-length string, "".

I interpret this to mean that there is no difference between "" and String.Empty. Why are you trying to distinguish between them?

recursive
I was not sure that there was a difference between "" and String.Empty with respect the the culture (unicode ascii etc.).
maxfridbe
+5  A: 

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.

Michael Burr
I didn't even bother to try this so case null works hmmm...
JoshBerke
+2  A: 

An empty string is "", which is equal to String.Empty. The reason that you can put "" in a case statement but not "String.Empty" is that "Empty" is a field of the class "String" and "" is actually a contant value.

Constant values are allowed in cases, String.Empty is a field and could be altered at run time. (In this case it will remain the same, but not all static fields of each class are constant values.)

In the case of 'if', that condition is evaluated at run time and if does not require a constant value.

I hope this explains why.

Jeroen Landheer
+1  A: 

Something that I just noticed is that you can combine if/else and switch statements! Very useful when needing to check preconditions.

if (string.IsNullOrEmpty(text))
{
    //blah
}
else switch (text)
{
    case "hi":
     Console.WriteLine("How about a nice game of chess?");
     break;
    default:
     break;
}
Even Mien
Someone pointed out on a related post on SO that this is not really combining the statements, but the else-clause is just without the curly braces, so the switch is the only statement executed. This is syntactically the same as Josh's answer. I do think it looks nice though.
Even Mien