C# - Difference between Convert.tostring and .tostring() method Example

C# - Difference between Convert.tostring and .tostring() method Example

Introduction:

In this article I will explain differences between .tostring() and Convert.tostring() in ASP.NET

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.
Example
//Returns a null reference exception for str.
string str;
object i = null;
str = i.ToString();
//Returns an empty string for str and does not throw an exception. If you dont
string str;
object i = null;
str = Convert.ToString(i);
I hope it helps.
Click: ASP.NET INTERVIEW QUESTIONS

1 comment:

We are here to listen you, Comment your valueable opinion...!!!