.Net

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);

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker