MSDN Architecture Center >
Architecture Forums
>
Architecture General
>
What is the difference between Convert.toString and .toString()
What is the difference between Convert.toString and .toString()
- What is the difference between Convert.toString and .toString()
Answers
- Just to give an understanding of what the above question means seethe below code.
int i =0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i));
We can convert the integer “i” using “i.ToString()” or “Convert.ToString” so what’s the difference.
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. - Hi
There is no difference between i.ToString() and Convert.ToString(i).
This is the body of the Convert method:
public static string ToString(int value)
{
return value.ToString();
}
Furthermore, the integer variable cannot be null since its a value type.
All Replies
- Just to give an understanding of what the above question means seethe below code.
int i =0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i));
We can convert the integer “i” using “i.ToString()” or “Convert.ToString” so what’s the difference.
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. - Hi
There is no difference between i.ToString() and Convert.ToString(i).
This is the body of the Convert method:
public static string ToString(int value)
{
return value.ToString();
}
Furthermore, the integer variable cannot be null since its a value type. Kuju wrote: Hi
There is no difference between i.ToString() and Convert.ToString(i).
This is the body of the Convert method:
public static string ToString(int value)
{
return value.ToString();
}
Furthermore, the integer variable cannot be null since its a value type.Kuju is absolutely correct.But what will happen if tostring is to be used with an custom object
- Once again using the trusty reflector, the body of the Convert.ToString(object value)looks like this:
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
}
Therefore, when you try Convert.ToString() on a custum object, you are guaranteed not to get an ObjectReferenceNullException. On the other hand you might want to get an exception if you don't expect your object to be null. The difference between Convert.ToString and .ToString() as follows
Convert.ToString wont allow null values. Cannot convert null to String
Whereas .ToString accepts null values
Thanks,
Srinivasan.K
Sybrant Technologies Pvt. Ltd.,
- Srini,
i think its the other way round- Proposed As Answer byPradeep Agrawal Monday, December 14, 2009 3:06 PM
Offcourse there is a difference:
string s;
object o = null;
s = o.ToString();
//returns a null reference exception for s.string s;
object o = null;
s = Convert.ToString(o);
//returns an empty string for s and does not throw an exception.Just to give an understanding of what the above question means seethe below code.
int i =0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i));
We can convert the integer “i” using “i.ToString()” or “Convert.ToString” so what’s the difference.
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.
Muhammed Adnan is absolutely correct, so we use Convert.ToString() much more than ToString()...

