Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Following on from <a href="https://stackoverflow.com/a/10355772/2199">JaredPar's excellent overload resolution</a> answer - the question remains "why does <code>Convert.ToString(string)</code> return null, but <code>Convert.ToString(object)</code> return <code>string.Empty</code>"?</p> <p>And the answer to that is...because <a href="http://msdn.microsoft.com/en-us/library/astxcyeh.aspx" rel="nofollow noreferrer">the docs</a> say <a href="http://msdn.microsoft.com/en-us/library/1a2xyyx8.aspx" rel="nofollow noreferrer">so</a>:</p> <blockquote> <p>Convert.ToString(string) returns "the specified string instance; no actual conversion is performed."</p> <p>Convert.ToString(object) returns "the string representation of value, or String.Empty if value is null."</p> </blockquote> <p>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.</p> <ol> <li><code>System.Convert</code> has methods for converting every base type to <em>itself</em>. This is strange - since no conversion is needed or possible, so the methods end up just returning the parameter. <code>Convert.ToString(string)</code> behaves the same. I presume these are here for code generation scenarios.</li> <li><code>Convert.ToString(object)</code> has 3 choices when passed <code>null</code>. 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 <code>System.Convert</code> deals with value types - which have a default value.</li> <li>It's debatable whether returning null is more "correct", but string.Empty is definitely more usable. Changing <code>Convert.ToString(string)</code> means breaking the "no actual conversion" rule. Since <code>System.Convert</code> 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. </li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload