Coder Perfect

Why is the string type’s default value null rather than an empty string?


It’s a pain to have to check all of my strings for null before using methods like ToUpper(), StartWith(), and so on…

I wouldn’t have to test if the default value of string was the empty string, and it would feel more consistent with other value types like int or double, for example. It would also make sense to have NullableString>.

So, why did the C# designers chose null as the string’s default value?

Note: This is related to the question, but it focuses on why rather than what to do about it.

Asked by Marcel

Solution #1

Because string is a reference type, and all reference types have null as their default value.

This is compatible with reference types’ behavior. A check for a null reference should be in place before invoking their instance members.

It would be inconsequential to assign the default value to a reference type other than null.

NullableT> is a type that works with value types. The fact that Nullable was not introduced on the original.NET platform means that changing that rule would have resulted in a lot of broken code. (Photo credit: @jcolebrand)

Answered by Habib

Solution #2

Because string is a reference type, Habib is correct.

You don’t have to check for null every time you use it, which is a big plus. If someone sends a null reference to your function, you should generally throw an ArgumentNullException.

The trouble is, if you tried to call, the framework would throw a NullReferenceException for you. On a string, use ToUpper(). Remember that even if you test your arguments for null, this situation might still occur because any property or method on the objects supplied to your function as parameters may evaluate to null.

As a result, they supply String for checking for empty strings or nulls, which is a common task. String and IsNullOrEmpty() IsNullOrWhiteSpace() was created specifically for this reason.

Answered by Dave Markle

Solution #3

For what it’s worth, you could create an extension method:

public static string EmptyNull(this string str)
    return str ?? "";

This is now safe to use:

string str = null;
string upper = str.EmptyNull().ToUpper();

Answered by Tim Schmelter

Solution #4

In addition, as of C# 6.0, you might use the following.

string myString = null;
string result = myString?.ToUpper();

Null will be returned as a string.

Answered by russelrillema

Solution #5

Nulls and empty strings are fundamentally different. A null value is a value that isn’t present, while an empty string is a value that isn’t present.

Making assumptions about a variable’s “value,” in this example an empty string, will be as good as starting the string with any other value that will not result in a null reference problem.

Furthermore, if you send the handle to that string variable to other portions of the program, that code will have no means of knowing if you passed a blank value or forgot to populate the value of that variable.

Another situation where this would be problematic is when the string is a function return value. Because string is a reference type that can technically have null and empty values, the function can likewise return a null or empty value (there is nothing to stop it from doing so). Because there are two types of “lack of a value,” an empty string and a null, all code that calls this function must perform two checks. One is for null and the other is for empty.

In short, having only one representation for a particular state is always preferable. See the links below for further information on empty and null values.

When dealing with user input, there’s a difference between NULL and Empty.

Answered by Abbas Gadhia

Post is based on