Question

I have a code concatenating two string using plus operator. If two strings are null, sum of two strings is not null.

var strLeft = default(string);
var strRight = default(string);

var strSum = strLeft + strRight;

I want to distinguish String.Empty + default(string) and default(string) + default(string). Both concat operation result is String.Empty.

Why results of both total are String.Empty?

Was it helpful?

Solution 2

It's because null string is treated as empty string 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

OTHER TIPS

The + operator calls on string arguments are translated by the C# compiler to calls to the string.Concat method. And here's what the documentation of this method states:

An Empty string is used in place of any null argument.

So basically that's by design. The string.Concat method will never return null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top