C# – Difference between Convert.ToString and .ToString() method

In this post i will show you what is the main difference between Convert.ToString and .ToString() method.

The basic difference between them is “Convert.ToString(variable)” handles NULL values even if variable value become null but “variable.ToString()” will not handle NULL values it will throw a NULL reference exception error. So as a good coding practice using “convert” is always safe.

//Returns a null reference exception for Name.
string Name;
object i = null;
Name= i.ToString();

//Returns an empty string for name and does not throw an exception.
string Name;
object i = null;
Name = Convert.ToString(i);
0 0 vote
Article Rating

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x