Take the 2-minute tour ×

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

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

When I try and convert dataRow[i] to a string using ToString() then I receive an error. How do I fix this?

share|improve this question

marked as duplicate by Sven, Lucas, Phill, Wimmel, pimvdb Sep 3 '11 at 9:57


what is the type of dr[0]? –  Jonathan Henson Sep 3 '11 at 5:12
As far as I know, neither Tostring() nor tostring() exists. C# is case-sensitive; are you referring to ToString, or do you have some other user-defined functions? Show us your source code that causes the error, and show us the error. –  Keith Thompson Sep 3 '11 at 5:15
There may also be the "issue" of DbNull (which is not null); and it may be possible that you may need to use IsNull(col) otherwise the index ([col]) can fail IIRC. In any case, please include relevant information: "receive an error" is as useful as "doesn't work". –  user166390 Sep 3 '11 at 6:02
add comment

4 Answers

Basically both are used to convert a value to a String but there is a basic difference between them:

When we have an NULL object, Convert.ToString(Object); handles the NULL value whereas Object.ToString(); does not handle the NULL value and it throws NULL Reference Exception.

share|improve this answer
add comment

There is a simple but important difference between them…

ToString() raise exception when the object is null

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

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

(string) cast assign the object in case of null

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

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


share|improve this answer
add comment

First, Object.ToString() is a virtual function in the base class Object. Any class can override ToString() to provide its own implementation. Convert.ToString() is a static method that attempts to take many different arguments and convert them into a meaningful string. Also, Object.ToString() will fail if the object calling it is null.

In addition, Object.ToString() does not always convert the object to the string form that you might expect. For instance, the base function Object.ToString() will always return the fully qualified type name of the object. Any class may implement ToString() however it wishes and this does not necessarily have to be something meaningful.

share|improve this answer
add comment

There is a basic different between Convert.ToString and .Tostring. Convert.ToString will handle the Null exception but .Tostring will throw error

share|improve this answer
add comment
