Difference between Convert.ToString() and .ToString()


The main difference between these both are the fact that Convert.ToString() can handle NULL and avoid ObjectReferenceNullException, while .ToString() would break in such case.

It is always good to use the Convert object than the ToString()

Let’s have a look into the Convert.ToString() method. Using a reflector, i got hold of the Convert object below:

public static string ToString(object value, IFormatProvider provider)
{
IConvertible convertible = value as IConvertible
if(convertible != null)
{
return convertible.ToString(provider)
}
if(value != null)
{
return value.ToString();
}
return string.Empty
}

Well I guess this would clear up the reason for using the Convert object in you type conversion operations.

Feel free to leave a reply here...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s