71

The MSDN article on String Basics shows this:

string str = "hello"; string nullStr = null; string emptyStr = ""; string tempStr = str + nullStr; // tempStr = "hello" bool b = (emptyStr == nullStr);// b = false; string newStr = emptyStr + nullStr; // creates a new empty string int len = nullStr.Length; // throws NullReferenceException 

Why doesn't concatenating with null throw a null reference exception? Is it to make a programmer's life easier, such that they don't have to check for null before concatenation?

1
  • nullStr.Length; throws NullReferenceException, because it asks a property of nullObject , that is not available. It s quite different to operations with the null object, so you shouldn’t expect similar behavior. Commented Mar 20, 2024 at 11:13

5 Answers 5

65

From MSDN:

In string concatenation operations, the C# compiler treats a null string the same as an empty string, but it does not convert the value of the original null string.

More information on the + binary operator:

The binary + operator performs string concatenation when one or both operands are of type string.

If an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object.

If ToString returns null, an empty string is substituted.

Sign up to request clarification or add additional context in comments.

2 Comments

The question was ultimately about the language design rationale. Some people have tried to make guesses in other answers. We'd need someone from MS to answer here.
Excellent choice by the C# team as it turned out to work like a charm with the C# 6 null-conditional operator especially when coupled with the C# 6 string interpolation features: e.g. return $"{person?.FirstName} {person?.LastName}"
6

I agree that conceptually strings are just values. However, consider the following code:

int? i = null; i += 1; // The result of this is that i == null 

If the other value type operators used default() the way the string operators are converting null to "", your explanation would make sense.

It's simplest to say that the string operators are a shortcut (special case) for convenience.

Comments

3

Conceptually, strings are normally thought of as values as opposed to references to objects which have identity. One of the main reasons that they aren't structs with value semantics is because of the overhead that comes with copying-on-assignment. If strings were values they couldn't be nullable and so a null is just treated by the "+" operator as if it were an empty string (i.e., as if default(string) == "" just as default(int) == 0).

Comments

1

I guess the language (or standard library) designers decided this would be a common enough case that they'd do programmers a favour.

(Neat! I always just assumed concating with null would through an exception!)

Comments

0

The reason it doesn't throw a null reference exception is because you're not actually trying to access any properties or methods on the null object. As CMS quotes, when concatenating string, nulls are replaced with empty strings.

1 Comment

This this what it does, not why it does this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.