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

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Check Also
Close
Back to top button