Thursday, October 23, 2008

Difference between .ToString() and Convert.ToString

Both converts the specified value to its equivalent String representation.

Convert.Tostring handles null value and returns string.Empty.

But Tostring doesn't handle null value and throws a NullReferenceException.

object obj = null;
string b = Convert.ToString(obj); //value of b will be ""
string a = obj.ToString(); // throws NullReferenceException

No comments: