Click here to Skip to main content
15,881,833 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Can anyone tell me, What is the Difference between Object.ToString() and Convert.ToString()?


please don't put links over here....
Thanks in advance
Posted

http://social.msdn.microsoft.com/forums/en-US/architecturegeneral/thread/bf118337-6dbd-4edd-9147-fbbcba7ce6c5/[^]

Well i saw lot of people saying object.ToString() doesn't handle Null values, i'll say if it's null it cannot handle anything. i.e.

object a = null;
a.AnyFunction(); 

this will raise an exception because object reference is not set to an instance of an object it has nothing to do with ToString() method. it's the CLR who will raise the exception because the object is null

on the other hand Convert is a helper class which has a function ToString and the function body is something like this:
updated: after @parmar_punit's comment.
public static string ToString(object obj)
{
    if(obj !=null)
    {
        return obj.ToString();
    }
return String.Empty;
}


Edit:
one more thing if the object in object.Tostring() is value type there will be no difference becasue value member got there memory intitialized at time of declaration if they are not declared as null.

Thanks,
Hemant
 
Share this answer
 
v4
Comments
Kim Togo 16-May-11 5:46am    
Basic the same as Pratik, 4. No code samples.
Hemant__Sharma 16-May-11 6:05am    
Well Thanks Kim.
Hemant__Sharma 16-May-11 6:12am    
i've improved my solution.
Thanks,
Hemant
parmar_punit 16-May-11 7:38am    
Hi Hemant....
Nice answer...
I would like to say something about your post, The ToString() method of Convert class is returning string.Empty if object is null and you have returning null over here...
Hemant__Sharma 16-May-11 8:32am    
aahhh my bad.. Thanks for reminding me.. yes i didn't notice that. I cannot give 5 to your comment :) bcz CP doesn't have such feature yet.
The Basic Difference is that,

Object.ToString() not Handle Null Values where as Convert.ToString() can handle Null Values.That means Convert.Tostring() will not generate Null Refernce Exception where as Object.ToString() will generate Null Refernce Exception if you are trying to do .ToString() on object which is NULL.

Example :

MIDL
nt x =0;
string s=x.ToString();
string Str=Convert.ToString(x);


MSIL
Here We can convert the integer "x" using "x.ToString()" or "Convert.ToString()" . But only The basic difference between them is "Convert" function handles NULLS while "x.ToString()"
does not it will throw a NULL reference exception error. So as for good programming practice using "Convert" is always safe.


For More Information refer below link:

[^]
 
Share this answer
 
v3
Comments
Kim Togo 16-May-11 5:46am    
My 5. I was not aware of this, I learn something :-)
parmar_punit 16-May-11 5:54am    
Thanks dear
As You said No Links. But instead of Putting this Question here You Can Simply Do Let Me google [^]

This first ans would be same as ans given below..... Or n Number Of ans..



There is a simple but important difference between these three…

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 this answer
 
Comments
Kim Togo 16-May-11 5:47am    
A 5.
Mahendra.p25 16-May-11 5:51am    
Thanks
parmar_punit 16-May-11 5:54am    
Thanks Mahendra
Mahendra.p25 16-May-11 6:17am    
WC
Object.ToString() return value of ToString() method if overriden in Class otherwise it will return Object Type, as all class inheriting from Object Class.

Convert.ToString(), converts to a particular format.

Regards
Rushi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900