none
Convert.ToString vs .toString()

    Question

  • Hi,

    I always get confused between the two.

    Please guide which to use when?

    Thanks!


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful
    Tuesday, January 10, 2012 12:39 PM

Answers

  • That's not entirely true.  Convert.ToString is similar to ToString but they do different, although equivalent, things.   In most cases Convert will call ToString on the value but in the general case of calling it on an object Convert actually defers to IConvertible if it is implemented.  This is important because IConvertible does not generally map to ToString.  ToString is generally used for a quick and friendly string representation of an object but IConvertible is designed for conversion so the output can vary.

    Additionally Convert generally calls the ToString overload of the primitives that pass the format provider.  This means that, in some cases, the returned value of Convert will be culture-specific whereas the parameterless ToString() version may not. 

    Michael Taylor - 1/10/2012
    http://msmvps.com/blogs/p3net

     

    Tuesday, January 10, 2012 2:59 PM
    Moderator
  • Adding one more.

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

    I had never given importance to the above difference between ToString and Covert.ToString. But, today, I saw this perticular problem in one of my associate's code, where he was reading an integer value (which could be null in DB) and trying to call ToString() on it. Such things usually miss out during code review if we are unaware of such differences.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, January 10, 2012 3:11 PM

All replies

    • Proposed as answer by Rajasekhar.R Tuesday, January 10, 2012 1:05 PM
    Tuesday, January 10, 2012 12:50 PM
  • No difference at all. The Convert.ToString method calls the toString method of the parameter and returns it.
    Be sure to up-vote me if I helped you. :) E-mail me if you need some extensive help. Contact: matija.cupic.97@gmail.com
    Tuesday, January 10, 2012 1:55 PM
  • That's not entirely true.  Convert.ToString is similar to ToString but they do different, although equivalent, things.   In most cases Convert will call ToString on the value but in the general case of calling it on an object Convert actually defers to IConvertible if it is implemented.  This is important because IConvertible does not generally map to ToString.  ToString is generally used for a quick and friendly string representation of an object but IConvertible is designed for conversion so the output can vary.

    Additionally Convert generally calls the ToString overload of the primitives that pass the format provider.  This means that, in some cases, the returned value of Convert will be culture-specific whereas the parameterless ToString() version may not. 

    Michael Taylor - 1/10/2012
    http://msmvps.com/blogs/p3net

     

    Tuesday, January 10, 2012 2:59 PM
    Moderator
  • Adding one more.

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

    I had never given importance to the above difference between ToString and Covert.ToString. But, today, I saw this perticular problem in one of my associate's code, where he was reading an integer value (which could be null in DB) and trying to call ToString() on it. Such things usually miss out during code review if we are unaware of such differences.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, January 10, 2012 3:11 PM
  • Thanks everyone! I got the difference.

    Now please let me know which to use when?

    Why microsoft has provided 2 ways of conversion... if Convert is a better way of doing it..?

     


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful
    Wednesday, January 11, 2012 4:25 AM
  • Hi,

    The difference between the 

    Convert.ToString vs .toString()

    Consider the Example below.

    string str;
    object o = null;
    str = o.ToString();
    //returns a null reference exception for  str .

    string  str ;
    object o = null;
    str = Convert.ToString(o);
    //returns an empty string for  str and does not throw an exception. 


    Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R
    Wednesday, January 11, 2012 4:28 AM
  • Thanks everyone! I got the difference.

    Now please let me know which to use when?

    Why microsoft has provided 2 ways of conversion... if Convert is a better way of doing it..?

     


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful

     “Convert” function handles NULLS while “integer.ToString()” does not it will throw a NULL reference exception error.

      So as good coding practice using “convert” is always safe.


    Don't forget to mark the post as answer or vote as helpful if it does, Regards - Rajasekhar.R
    Wednesday, January 11, 2012 4:30 AM
  • The preferred approach is to use ToString() on the object in question.  The use of Convert should be rare in well written code (IMHO).  Convert was a 1.x class to help convert types.  In later versions better functionality is available such that Convert should be used only when necessary (such as object to type).  For conversion to string use T.ToString() (that's why everybody gets one).  For conversion from string use the appropriate T.Parse or T.TryParse methods.

    The only time you might consider Convert.ToString() is if you are dealing with a potentially null object but I find the following code nicer:  (obj != null) ? obj.ToString() : "" than Convert.ToString(obj).  Just my personal preference though. 

    Michael Taylor - 1/11/2012
    http://msmvps.com/blogs/p3net

    Wednesday, January 11, 2012 2:49 PM
    Moderator