Difference between Convert.toString and .toString() method.


To understand both the methods let's take an example:

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

Here both the methods are used to convert the string but 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
.
 
Let's see another example:


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.
Ebook Download
View all
Learn
View all
F11Research & Development, LLC