views:

48

answers:

2

Possible Duplicate:
Difference between Convert.tostring() and .tostring()

Hi

Carrying on from this question http://stackoverflow.com/questions/3486810/the-difference-between-convert-and-parse

Here are two lines of code.

Convert.ToString(myObject);
myObject.ToString();

My question is what is the difference and which would be best to use?

Thank you in advance.

+3  A: 

myObject.ToString() could throw a NullReferenceException, where Convert.ToString will never do that.

leppie
+7  A: 

The basic difference between them is Convert function handles NULLS while i.ToString() does not it will throw a NULL reference exception error. So as good coding practice using Convert is always safe.

Ardman
Just to clarify - Convert.ToString(null) would return null?
Ash Burlaczenko
That is correct.
Ardman
@Ash, yes it returns null although one might reasonably expect it to return the empty string.
Paul Ruane