Difference between .Tostring() and convert.Tostring()
Difference between .Tostring() and convert.Tostring() explain with example.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
rahul jaiswalPosted Aug 27, 2012, 8:11 AM
ToString()on an object presumes that the object is not null (since an object needs to exist to call an instance method on it).Convert.ToString(obj)doesn't need to presume the object is not null (as it is a static method on the Convert class), but instead will returnString.Emptyif it is null.Jignesh TrivediPosted Aug 27, 2012, 8:10 AM
ToString() raise exception when the object is null
Convert.ToString() return string.Empty in case of null object
string o = NullObject.ToString(); // Give runtime exception.
string o1 = Convert.ToString(NullObject); // not give any runtime exception.
hope this will help you.
Girish BarjePosted Aug 27, 2012, 7:26 AM
Convert.ToString()handlesnull, whileToString()doesn't.hj jhPosted Aug 27, 2012, 7:25 AM
So as a good programming practice , use convert.tostring().
Please accept as answer if your problem resolved!