Sunday, August 16, 2009

Difference between Convert.ToString and .ToString()

The following example describes the difference.


int i =0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i));

We can convert the integer “i” using “i.ToString()” or “Convert.ToString”.
The basic difference between “i.ToString()” and “Convert.ToString” is “Convert” function handles NULLS while “i.ToString()” does not and it will throw a NULL reference exception error. Hence the good coding practice is using “convert” which is always safe.

0 comments:

.