C# Switch with String.IsNullOrEmpty
Asked Answered
W

9

18

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
Warner answered 11/1, 2009 at 1:56 Comment(6)
I didn't know there was a difference between String.Empty and "". What's the difference?Opening
There are none. It's the same at the content level. The reference is different. String.Empty returns always the same instance while "" return a different instance.Umbel
I'm preety sure two "" will actually be the same object since .net interns the strings, but not positive. object.ReferenceEquals indicates they are the same.Bianco
My test agrees with Josh's - the literal empty string, "", refers to the same object as String.Empty. I'm not sure this is guaranteed by any standard (though interning of literals is guaranteed - not sure if that guarantee applies to String.Empty), but it's certainly what happens now.Bendite
Now the question is... what if it's in separate assembly or in separate methods/class/etc. Personally. I use String.Empty because it's more readable than "".Umbel
note that if you want to test IsNullOrWhitespace, you have to do: switch (text) { case null: case string t when string.IsNullOrWhitespace(): ... break; } which slightly defeats the purpose of a switch for readabilityJaquesdalcroze
F
0

You can use pattern matching to test for string length (or other things). This code was tested in .NET 6 and .NET 7.

// Program.cs

Console.WriteLine(PatternMatch(null));
Console.WriteLine(PatternMatch(""));
Console.WriteLine(PatternMatch("Hello world!"));
Console.WriteLine(PatternMatch("Blah!"));
Console.WriteLine(PatternMatch("99 bottles of beer on the wall"));

string PatternMatch(string str)
{
    switch (str)
    {
        case null: return "String is null";
        case "": return "String is empty";
        case var blah when str.StartsWith("Blah"): return "Blerg";
        case var match when Regex.IsMatch(match, @"\d\d.*"): return "Starts with 2 digits";
        case { Length: > 0 }: return str;
        default: return "We should never get here";
    };
}

The output should be:

String is null
String is empty
Hello world!
Blerg
Starts with 2 digits

You can do the same thing using switch expressions too.

string PatternMatch2(string str) => str switch
{
    null => "String is null",
    "" => "String is empty",
    var blah when str.StartsWith("Blah") => "Blerg",
    var match when Regex.IsMatch(match, @"\d\d.*") => "Starts with 2 digits",
    { Length: > 0 } => str,
    _ => "We should never get here"
};
Farra answered 17/5, 2023 at 4:15 Comment(0)
U
32

I would suggest something like the following:

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

Is that what you are looking for?

Umbel answered 11/1, 2009 at 2:13 Comment(0)
B
25

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.

Bendite answered 11/1, 2009 at 3:23 Comment(0)
B
6

how about

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

}

Bianco answered 11/1, 2009 at 2:5 Comment(3)
The question was to avoid if-else statements.Narrate
If you look at the edit history the original question didn't include anything about if else;-)Bianco
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..Bianco
O
6

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?

Opening answered 11/1, 2009 at 2:17 Comment(1)
I was not sure that there was a difference between "" and String.Empty with respect the the culture (unicode ascii etc.).Warner
K
4

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.

Kommunarsk answered 11/1, 2009 at 5:45 Comment(0)
Q
3

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;
}
Quinta answered 2/10, 2009 at 13:54 Comment(1)
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.Quinta
H
3

With new c# features, you can use switch expression syntax

       text switch
       {
           "" or null => "a",
           _ => "b"
       };
Horse answered 10/12, 2021 at 17:11 Comment(1)
Similarly, It seems you can also use the 'or' with classic case statement as such: case null or "":Beaumont
R
0
string StrMode;
if (!string.IsNullOrEmpty(StrMode))
{  
    switch (StrMode.Trim())
    {
        case "Souse":
        {
             //Statement Eg:  
             MesssageBox.Show("Souse");
             break;
        }

        case "Company Agent":
        {
             //Statement Eg:
             MesssageBox.Show("Souse");
             break; 
        }

        default:
             return;
    }
}
Rimskykorsakov answered 8/1, 2013 at 7:2 Comment(0)
F
0

You can use pattern matching to test for string length (or other things). This code was tested in .NET 6 and .NET 7.

// Program.cs

Console.WriteLine(PatternMatch(null));
Console.WriteLine(PatternMatch(""));
Console.WriteLine(PatternMatch("Hello world!"));
Console.WriteLine(PatternMatch("Blah!"));
Console.WriteLine(PatternMatch("99 bottles of beer on the wall"));

string PatternMatch(string str)
{
    switch (str)
    {
        case null: return "String is null";
        case "": return "String is empty";
        case var blah when str.StartsWith("Blah"): return "Blerg";
        case var match when Regex.IsMatch(match, @"\d\d.*"): return "Starts with 2 digits";
        case { Length: > 0 }: return str;
        default: return "We should never get here";
    };
}

The output should be:

String is null
String is empty
Hello world!
Blerg
Starts with 2 digits

You can do the same thing using switch expressions too.

string PatternMatch2(string str) => str switch
{
    null => "String is null",
    "" => "String is empty",
    var blah when str.StartsWith("Blah") => "Blerg",
    var match when Regex.IsMatch(match, @"\d\d.*") => "Starts with 2 digits",
    { Length: > 0 } => str,
    _ => "We should never get here"
};
Farra answered 17/5, 2023 at 4:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.