0

I had an issue related to System.Convert(Object value) method, my research led me to this documentation.
I am wondering what are the reasonsbehind this decision that c# team has made, because this doesn't make any sense to me.
From my point of view its better to return "Null", please pay attention to this example:

static void Main(string[] args)
    {
        int? ID = null; 
        string result = Convert.ToString(ID);
        Console.WriteLine("{0} ,{1}",result ,result.Length);
        Console.ReadKey();
    }
//result would be "" and the result.Length=0

As you can see ID is null but convert(ID) is not nulland it sounds weird to me !!
This is source code of convert(object value)I picked it up from this answer of the question.

public static string ToString(Object value) {return ToString(value,null);}

public static string ToString(Object value, IFormatProvider provider) { 
    IConvertible ic = value as IConvertible; 
    if (ic != null)
        return ic.ToString(provider); 
    IFormattable formattable = value as IFormattable;
    if (formattable != null)
        return formattable.ToString(null, provider);
    return value == null? String.Empty: value.ToString();

Please pay attention to the last line. I just want to know what is the rational behind that.
Thank you in advance.

Edit

There are so many cases that ID.Tostring() is not useful at all
for example :

public System.Web.Mvc.ActionResult MyAction(int? Id)
{
//This line of code dosent show "Id has no value" at all 
MyContetnt = (System.Convert.ToString(Id) ?? "Id has no value");

return Content(MyContetnt);
}

In this code example, If you run this Mvc sample through this path :
http://localhost: portnumber/ControllerName/MyAction
I mean without using ID in the path You see Nothing on the screen because ID is null here and the resault of Convert.Tostring(ID) is System.Empty or "" .

I mean in this case there is no way to refactor this code using null-coalescing-operator, By using ID.tostring() a nullrefrenceException would be raised,That is not useful and is not my intention. I know that its easy to say :

MyContent = Id.HasValue ? Id.Value.ToString() : "Id has no value" 
// this line works fine;

But suppose that The result of convert.ToString(Null) was Null Then the first code featured with null-coalescing-operator would work perfectly.

so I am just curious that what is the reason behind this idea that the result of
Convert.tostring (object null) is string.empty not null?

Community
  • 1
  • 1
siamak
  • 669
  • 1
  • 10
  • 28
  • You can use .ToString() this will raise an error `object reference not set to an instance of an object` – Khurram Ali Mar 26 '15 at 01:20
  • in your case `string result = (ID).ToString();` will return exception which is `object reference not set to an instance of an object` – Khurram Ali Mar 26 '15 at 01:27
  • Maybe because null isn't a string, and the authors decided the closest string representation of null was an empty string? – Rufus L Mar 26 '15 at 05:28

3 Answers3

2

All the Convert methods work this way. That's the only reason why you'd use them in the first place.

For example, consider Convert.ToInt32. If you pass null, you will get 0. If you don't want that, you can use int.Parse instead - that will throw on null values.

The same way, Convert.ToString is the safe way of getting a string out of any value, including null. If you want null to throw, you can use object.ToString(). You might argue that default(string) is null, rather than string.Empty, but null is not a safe value in this sense - you can't work further with null, other than by checking for null explicitly. On the other hand, string.Empty still allows you to concatenate, or ask for length, or IndexOf, or...

The whole point is that the Convert class is guarded against null values, it goes back to defaults instead. It's not a technical reason, it's there to make (certain ways of) programming easier.

Luaan
  • 62,244
  • 7
  • 97
  • 116
0
return value == null? String.Empty: value.ToString();

In your case, value == null so the result will be string.Empty. And it is the same for the method ID.ToString()

public override string ToString()
{
  if (!this.HasValue)
    return ""; //return empty string when value is null
  else
    return this.value.ToString();
}

But if you want to return null when id is null, write an extension for this.

public static class Extension {
    public static string ToStringWithChecking(this int? param) {
        if (!param.HasValue) {
            return null;// or throw exception here
        }
        return param.ToString();
    }
}
toannm
  • 485
  • 4
  • 9
  • 1
    Thank you for your effort but I am looking for some technical reasons or design based reasons of such a decision. – siamak Mar 26 '15 at 10:02
-1

The statement in your question is incorrect. Convert.ToString(object value) does return null when value is null. This

int? ID = null;
Console.WriteLine(Convert.ToString(ID) == string.Empty);
Console.WriteLine(Convert.ToString(null) == null);
object box = ID;
Console.WriteLine(Convert.ToString(box) == string.Empty);

Writes true true true.

http://ideone.com/jRsefy

In your example, you're not passing null, you're passing a reference to a boxed Nullable which is null.

From the documentation you linked to (I had to read it twice):

The string representation of value, or String.Empty if value is an object whose value is null. If value is null, the method returns null.

Note: the question did not specify the Framework version, so I assume 4.5. Apparently the behavior changed from 4.0 to 4.5, so the answer to the question is: they realized it was a bad idea and changed it.

Henrik
  • 23,186
  • 6
  • 42
  • 92
  • statement is totally true check this out http://stackoverflow.com/questions/13514337/convert-tostring-behaves-differently-for-null-object-and-null-string/13514470#13514470 – siamak Mar 26 '15 at 11:45
  • @siamak Are you saying `Convert.ToString(null) != null`? Did you check? – Henrik Mar 26 '15 at 12:02
  • please see my response in the answers part. – siamak Mar 26 '15 at 22:37