Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends


i am using winform application,in converting concepts when i am using Conver.ToSring() and .ToString().can you please explain me.


Thanking U.
Posted

Convert.ToString() it will handle the null exceptions and using .ToString() It won't handle the null exceptions.

find below,
C#
Object obj = null;
String objValue1 = obj.ToString(); //throws Null reference exception
String objValue2 = Convert.ToString(obj);  //returns NULL


:)
 
Share this answer
 
Hi Sheker,

Main difference is each class base class is Object class, and object class has few methods and ToString() is one of them you can write overload and override .

So when you call object.ToString() its mean it will call that objects ToString
there for if object is null then it will throw an exception Object is not set to an instance of an object.

Where as In Convert.ToString method, Convert is a static class where we pass our object and get the out put in that method its check where the object is null or no if not the call that objects ToString method other wise return null.

This is the main difference Now you could understand why this behavioral difference

C#
object objec = null;
string objValue1 = objec .ToString(); //Exception
string objValue2 = Convert.ToString(objec); //return Null
 
Share this answer
 
 
Share this answer
 
Hello dude .............


C#
object objec = null;
string objValue1 = objec .ToString(); //It is does not handle Null Value and throws Null reference exception
string objValue2 = Convert.ToString(objec); //It is  handle Null Value and  output Null
 
Share this answer
 
v2
hi sekar raja,

the main difference bet. convert.tostring() and .tostring() is that convert.tostring()accept NULL values and returns it whereas .tostring() throws Null reference exception.

let me know if you have any query.

thanks...
 
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