Difference between Convert.ToString () and .ToString () in C #?

Possible duplicate:
variable.ToString () vs. Convert.ToString (variable)

What is the difference between Convert.ToString () and .ToString () in C #?

When I try to convert dataRow[i] to a string using ToString (), I get an error. How to fix it?

+7
source share
4 answers

Mostly both are used to convert a value to String, but there is a major difference between them:

When we have a NULL object, Convert.ToString(Object); processes a NULL value, while Object.ToString(); does not process a NULL value and throws a NULL Reference Exception.

+15
source

There is a simple but important difference between the two ...

ToString () throws an exception when an object is null

So, in the case of object.ToString (), if the object is null, it throws a NullReferenceException.

Convert.ToString () returns string.Empty in case of a null object

(string) cast assigns an object if null

So, in the case of MyObject o = (string) NullObject;

But when you use o to access any property, it will throw a NullReferenceException.

http://maniish.wordpress.com/2007/10/08/difference-between-tostring-vs-converttostring-vs-string-cast/

+8
source

First, Object.ToString () is a virtual function in an object of a base class. Any class can override ToString () to provide its own implementation. Convert.ToString () is a static method that tries to use many different arguments and convert them to a meaningful string. In addition, Object.ToString () will fail if the object calling it is zero.

In addition, Object.ToString () does not always convert the object to the string form you can expect. For example, the base function Object.ToString () always returns the fully qualified name of an object type. Any class can implement ToString (), however it wants to, and this does not have to be significant.

+7
source

There is a basic difference between Convert.ToString and .Tostring. Convert.ToString will handle a Null exception, but .Tostring will throw an error

+1
source

All Articles