C# adding string + null doesn't throw an error?
<p>As a programmer I would have expected this to throw an exception. Is there a reason why it treats null as ""?</p>

<pre><code>string s = "hello";
string t = null;

Console.WriteLine(s + t);
</code></pre>

<p>Output:</p>

<blockquote>
  <p>hello</p>
</blockquote>

<p>Just to emphasise my question, do you know <em>why the decision to turn null into String.Empty was made</em>? I was expecting some string to be added but there was a problem further back where it wasn't retrieving it properly and it came back with null. The problem went unnoticed!</p>

<p>Here's a pseudocode of why I think it is bad (and why I was shocked to discover no error):</p>

<p>You can assume I overloaded ToString to give the name of the person or details about them.</p>

<pre><code>Person p = PersonDatabase.GetForName("Jimmy Gibbs");

Console.WriteLine("Name is: " + p.ToString());
</code></pre>

<p>Throws an exception because p is null</p>

<pre><code>String p = PersonDatabase.GetForName("Jimmy Gibbs").Name;

Console.WriteLine("Name is: " + p);
</code></pre>

<p>Doesn't throw an exception, even though p is null (it treats it as "").</p>

<p>(If you want to be picky and say I won't be able to get Name as GetforName will be null, think of it as this:)</p>

<pre><code>String p = PersonDatabase.GetNameFromID(1234); // Returns a string or null if not found

Console.WriteLine("Name is: " + p);
</code></pre>

<p>(If you use this value in a list or array you'll end up with a blank entry which is what broke for me, it was creating javascript and trying to get element by ID of "")</p>
