C#
programming
null values
string manipulation
.NET

Why is it Valid to Concatenate Null Strings but not to Call null.ToString?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In C#, string concatenation and method calls treat null very differently. Concatenation works because it is implemented by helper methods that explicitly accept null, while null.ToString() fails because it tries to invoke an instance method on a reference that points to no object.

Concatenation Calls a Helper, Not a Method on null

When you write string concatenation, the runtime does not interpret it as "ask the left-hand string object to append the right-hand one." Instead, it uses string-concatenation logic such as string.Concat, and that helper treats null as an empty string.

csharp
1using System;
2
3public class Program
4{
5    public static void Main()
6    {
7        string left = null;
8        string right = "world";
9
10        string result = left + right;
11        Console.WriteLine(result); // world
12    }
13}

This works because the concatenation helper is designed to be forgiving. A missing string operand is treated as if it were "".

null.ToString() Requires an Actual Object

An instance method call is different. ToString() must run on an object instance, and null does not refer to any object at all.

csharp
1using System;
2
3public class Program
4{
5    public static void Main()
6    {
7        string value = null;
8        Console.WriteLine(value.ToString());
9    }
10}

That throws NullReferenceException because the runtime tries to dereference value in order to find the instance method implementation.

The key distinction is:

  • Concatenation can be implemented as a static helper that accepts null
  • Instance method calls require a real object reference

Why the Behavior Is Useful

Treating null as an empty string during concatenation makes many formatting operations easier. It avoids constant defensive checks in simple display code.

But allowing null.ToString() would be much more dangerous because it would hide the fact that there is no object there. In many cases, that missing object is a bug you want to notice immediately.

Safer Alternatives When a Value May Be null

If a value may be missing and you still want text output, use null-aware code explicitly.

csharp
1using System;
2
3public class Program
4{
5    public static void Main()
6    {
7        object value = null;
8
9        Console.WriteLine(value?.ToString() ?? "<missing>");
10    }
11}

Here, ?. avoids the dereference and ?? supplies a fallback value. That is much clearer than relying on implicit behavior when the code path matters.

You can also use interpolation safely with a fallback:

csharp
string name = null;
Console.WriteLine($"User: {name ?? "<unknown>"}");

A Good Mental Model

Think of concatenation as an operation over values, not as a method call on the left operand. Think of ToString() as a request sent to a specific object instance. Once you separate those two ideas, the behavior stops looking inconsistent.

One is a tolerant helper function. The other is a true instance-method invocation.

That design is deliberate because it makes formatting convenient without pretending that a missing object is still safe to dereference.

Common Pitfalls

  • Assuming + on strings means "call a method on the left string" leads to the wrong intuition.
  • 'null.ToString() fails because null is not an object with methods.'
  • Even though concatenation tolerates null, relying on that behavior everywhere can hide missing-data bugs.
  • Use ?. and ?? when you want explicit, readable null handling.

Summary

  • String concatenation in C# is implemented to treat null as an empty string.
  • 'null.ToString() throws because instance methods require a real object reference.'
  • The difference is between a tolerant helper operation and a true dereference.
  • Prefer explicit null-safe operators when the absence of a value matters to your logic.

Course illustration
Course illustration

All Rights Reserved.