In this post, we will explain the difference between .ToString() and Convert.ToString() methods in C#.
The basic difference between them is “Convert.ToString()” handles NULL values while “.ToString()” doesn’t. Which means if the variable being passed to Convert.ToString() is NULL, it won’t throw an exception.
Whereas, “.ToString()” does not handle NULL values and it will throw a NULL reference exception.
So as a good coding practice using “Convert.ToString()” is always safe.
Difference between Convert.ToString() and ToString() with example
Let’s see this with the help of an example:
//Returns a null reference exception for str. string name; object o = null; name = o.ToString(); //Returns an empty string for name //and does not throw an exception. string name; object o = null; name = Convert.ToString(o);