Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Convert.ToString(null) return a different value if you cast null?

Tags:

c#

.net

People also ask

What happens if you do a ToString on a null?

.ToString() It will not handle NULL values; it will throw a NULL reference exception error.

What is the difference between ToString () and convert ToString ()?

Here both the methods are used to convert the string but the basic difference between them is: "Convert" function handles NULLS, while "i. ToString()" does not it will throw a NULL reference exception error. So as good coding practice using "convert" is always safe.

Can ToString return null C#?

No method can be called on a null object. If you try to call ToString() on a null object it will throw a NullReferenceException . The conclusion “if it returns null still then the object is null” is not possible and thus wrong.


There are 2 overloads of ToString that come into play here

Convert.ToString(object o);
Convert.ToString(string s);

The C# compiler essentially tries to pick the most specific overload which will work with the input. A null value is convertible to any reference type. In this case string is more specific than object and hence it will be picked as the winner.

In the null as object you've solidified the type of the expression as object. This means it's no longer compatible with the string overload and the compiler picks the object overload as it's the only compatible one remaining.

The really hairy details of how this tie breaking works is covered in section 7.4.3 of the C# language spec.


Following on from JaredPar's excellent overload resolution answer - the question remains "why does Convert.ToString(string) return null, but Convert.ToString(object) return string.Empty"?

And the answer to that is...because the docs say so:

Convert.ToString(string) returns "the specified string instance; no actual conversion is performed."

Convert.ToString(object) returns "the string representation of value, or String.Empty if value is null."

EDIT: As to whether this is a "bug in the spec", "very bad API design", "why was it specified like this", etc. - I'll take a shot at some rationale for why I don't see it as big deal.

  1. System.Convert has methods for converting every base type to itself. This is strange - since no conversion is needed or possible, so the methods end up just returning the parameter. Convert.ToString(string) behaves the same. I presume these are here for code generation scenarios.
  2. Convert.ToString(object) has 3 choices when passed null. Throw, return null, or return string.Empty. Throwing would be bad - doubly so with the assumption these are used for generated code. Returning null requires your caller do a null check - again, not a great choice in generated code. Returning string.Empty seems a reasonable choice. The rest of System.Convert deals with value types - which have a default value.
  3. It's debatable whether returning null is more "correct", but string.Empty is definitely more usable. Changing Convert.ToString(string) means breaking the "no actual conversion" rule. Since System.Convert is a static utility class, each method can be logically treated as its own. There's very few real world scenarios where this behavior should be "surprising", so let usability win over (possible) correctness.