59

I am wondering if there is a special method/trick to check if a String object is null. I know about the String.IsNullOrEmpty method but I want to differentiate a null String from an empty String (="").

Should I simply use:

if (s == null) {
    // blah blah...
}

...or is there another way?

Otiel
  • 18,404
  • 16
  • 78
  • 126
  • 3
    @Thilo: I don't. Just to learn the best ways of using C#. – Otiel Sep 26 '11 at 10:13
  • Actually, it was good that you asked. Seems to be straightforward in C#, but for example in Javascript there are a lot of nuances concerning null checks. – Thilo Sep 26 '11 at 10:15

5 Answers5

90

An object can't be null - the value of an expression can be null. It's worth making the difference clear in your mind. The value of s isn't an object - it's a reference, which is either null or refers to an object.

And yes, you should just use

if (s == null)

Note that this will still use the overloaded == operator defined in string, but that will do the right thing.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Thilo: No, I doubt it. The == implementation would have to be broken for that to help, and the calling code becomes uglier. – Jon Skeet Sep 26 '11 at 10:31
  • @Thilo: I would prefer not to work with broken code at all, rather than working with an == implementation which worked if the first argument is null but not the second. Who's to say it wouldn't be changed to work the other way round? Note that assuming a symmetric declaration, the same overload will still be called either way. – Jon Skeet Sep 26 '11 at 10:35
35

You can use the null coalescing double question marks to test for nulls in a string or other nullable value type:

textBox1.Text = s ?? "Is null";

The operator '??' asks if the value of 's' is null and if not it returns 's'; if it is null it returns the value on the right of the operator.

More info here: https://msdn.microsoft.com/en-us/library/ms173224.aspx

And also worth noting there's a null-conditional operator ?. and ?[ introduced in C# 6.0 (and VB) in VS2015

textBox1.Text = customer?.orders?[0].description ?? "n/a";

This returns "n/a" if description is null, or if the order is null, or if the customer is null, else it returns the value of description.

More info here: https://msdn.microsoft.com/en-us/library/dn986595.aspx

Oliver Slay
  • 1,075
  • 10
  • 11
  • This is one of those answers that probably isn't the exact answer the author was looking for, but it definitely complements my coding skills in c#. I always enjoy reading the other answers to find more than what I was looking for. – Tim Sanders Nov 11 '20 at 20:45
32

To be sure, you should use a function to check for null and empty as below:

string str = ...
if (!String.IsNullOrEmpty(str))
{
...
}
TomDane
  • 1,010
  • 10
  • 25
MrTo-Kane
  • 510
  • 5
  • 15
  • 5
    The author wants to check exactly for null (not null or empty). – kost Nov 03 '17 at 21:12
  • Great answer if somebody wants to check for both null and empty values, but that is not what the question ask here. – Gaurav Mall Mar 07 '20 at 18:47
  • Perhaps the author wanted to take a different code path in case the string was empty (and not null). – CMD Jan 30 '23 at 14:08
6

If you are using C# 7.0 or above you can use is null:

if (s is null) {
    // blah blah...
}

Also, note that when working with strings you might consider also using IsNullOrWhiteSpace that will also validate that the string doesn't contain only spaces.

Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
0

For .net 5 (probably also for .net Core 3.1)

Different possibility to write but always the same problem.

string wep = test ?? "replace";
Console.WriteLine(wep);

result: "replace"

or

string test=null;
test ??= "replace";
Console.WriteLine(test);
test="";
test??="replace";
Console.WriteLine(test);
  • first try: "replace"
  • second try: blank
string test="";
if(test is null)
    Console.WriteLine("yaouh");
else
    Console.WriteLine("Not yahouu");

Result: "Not yahou"

Vonkel.
  • 306
  • 3
  • 12
  • This is also possible for .NET Framework. `object is null` is available since C# 7 specification. – Otiel Apr 14 '21 at 08:13