How to check if String is null

C#Null

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

string str = ...
if (!String.IsNullOrEmpty(str))
{
...
}

Solution 3 - C#

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

Solution 4 - C#

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.

Solution 5 - C#

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"

Solution 6 - C#

You can check with null or Number.

First, add a reference to Microsoft.VisualBasic in your application.

Then, use the following code:

bool b = Microsoft.VisualBasic.Information.IsNumeric("null");
bool c = Microsoft.VisualBasic.Information.IsNumeric("abc");

In the above, b and c should both be false.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOtielView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#MrTo-KaneView Answer on Stackoverflow
Solution 3 - C#Oliver SlayView Answer on Stackoverflow
Solution 4 - C#Misha ZaslavskyView Answer on Stackoverflow
Solution 5 - C#Vonkel.View Answer on Stackoverflow
Solution 6 - C#Dinesh Kumar SharmaView Answer on Stackoverflow