75

I usually use something like this for various reasons throughout an application:

if (String.IsNullOrEmpty(strFoo))
{
     FooTextBox.Text = "0";
}
else
{
     FooTextBox.Text = strFoo;
}

If I'm going to be using it a lot I will create a method that returns the desired string. For example:

public string NonBlankValueOf(string strTestString)
{
    if (String.IsNullOrEmpty(strTestString))
        return "0";
    else
        return strTestString;
}

and use it like:

FooTextBox.Text = NonBlankValueOf(strFoo);

I always wondered if there was something that was part of C# that would do this for me. Something that could be called like:

FooTextBox.Text = String.IsNullOrEmpty(strFoo,"0")

the second parameter being the returned value if String.IsNullOrEmpty(strFoo) == true

If not does anyone have any better approaches they use?

Adam Porad
  • 14,193
  • 3
  • 31
  • 56
user2140261
  • 7,855
  • 7
  • 32
  • 45
  • `FooTextBox.Text = foo.IsNullOrEmpty ? "0" : foo;` – Devin Burke Mar 27 '13 at 13:49
  • 2
    Use IsNullOrWhiteSpace as it trims the string. – Matija Grcic Mar 27 '13 at 13:54
  • 2
    I would not change your code, except to make `NonBlankValueOf` static. Don't rely on something that C# might provide - the method `NonBlankValueOf` has a specific meaning to your app, and you control that meaning. For example, what if you needed to change "0" to "1" someday? – default.kramer Mar 27 '13 at 13:54

6 Answers6

164

There is a null coalescing operator (??), but it would not handle empty strings.

If you were only interested in dealing with null strings, you would use it like

string output = somePossiblyNullString ?? "0";

For your need specifically, there is the conditional operator bool expr ? true_value : false_value that you can use to simplify if/else statement blocks that set or return a value.

string output = string.IsNullOrEmpty(someString) ? "0" : someString;
Pang
  • 9,564
  • 146
  • 81
  • 122
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • 2
    +1 for a good example of when to use null coalesc, although the OP may need it to catch empty string in his/her case. – Devin Burke Mar 27 '13 at 13:50
  • Is there a way to do this 2nd thing (string output = string.IsNullOrEmpty(someString) ? "0" : someString;), but if 'someString' is an expression, then capture the result of that expression and return it in the end? I haven't found anything on this yet, but find it would be very useful. – mochsner May 07 '21 at 07:01
16

You could use the ternary operator:

return string.IsNullOrEmpty(strTestString) ? "0" : strTestString

FooTextBox.Text = string.IsNullOrEmpty(strFoo) ? "0" : strFoo;
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
11

You can write your own Extension method for type String :-

 public static string NonBlankValueOf(this string source)
 {
    return (string.IsNullOrEmpty(source)) ? "0" : source;
 }

Now you can use it like with any string type

FooTextBox.Text = strFoo.NonBlankValueOf();
ssilas777
  • 9,672
  • 4
  • 45
  • 68
8

This may help:

public string NonBlankValueOf(string strTestString)
{
    return String.IsNullOrEmpty(strTestString)? "0": strTestString;
}
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
0

Old question, but thought I'd add this to help out,

#if DOTNET35
bool isTrulyEmpty = String.IsNullOrEmpty(s) || s.Trim().Length == 0;
#else
bool isTrulyEmpty = String.IsNullOrWhiteSpace(s) ;
#endif
dathompson
  • 65
  • 3
0

You can achieve this with pattern matching with the switch expression in C#8/9

FooTextBox.Text = strFoo switch
{
    { Length: >0 } s => s, // If the length of the string is greater than 0 
    _ => "0" // Anything else
};
Kevin Smith
  • 13,746
  • 4
  • 52
  • 77