Difference between convert.ToString() and object.ToString()


In this article I'm going to explain what is the Difference between convert.ToString() and object.ToString()? ToString is the major formatting method in .net framework. Convert.Tostring method is used to convert the spefied value to its equivalent string representaion. Object.Tostring method return string that represent the current object.

ToString is the major formatting method in .net framework.
Convert.Tostring method is used to convert the spefied value to its equivalent string representaion. Object.Tostring method return string that represent the current object. The basic difference between them is "Convert.ToString(variableName)" 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.

Example:-

---The following statement Returns a null reference exception for strname variable.
string strname;
object ivariable = null;
strname = ivariable.ToString();

---The following eturns an empty string for strname and does not throw an exception. If you dont
string strname;
object ivariable = null;
strname = Convert.ToString(ivariable);


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: