Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 16-Jul-12 21:45pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Convert.ToString() it will handle the null exceptions and using .ToString() It won't handle the null exceptions.
 
find below,
Object obj = null;
String objValue1 = obj.ToString(); //throws Null reference exception
String objValue2 = Convert.ToString(obj);  //returns NULL
 
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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
 
object objec = null;
string objValue1 = objec .ToString(); //Exception
string objValue2 = Convert.ToString(objec); //return Null
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello dude .............
 

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 17 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100