One liner for If string is not null or empty else
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;
You could use the ternary operator:
return string.IsNullOrEmpty(strTestString) ? "0" : strTestString
FooTextBox.Text = string.IsNullOrEmpty(strFoo) ? "0" : strFoo;