MSDN Architecture Center > Architecture Forums > Architecture General > What is the difference between Convert.toString and .toString()
Ask a questionAsk a question
 

AnswerWhat is the difference between Convert.toString and .toString()

Answers

  • Friday, November 03, 2006 7:57 PMMuhammad Adnan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Just to give an understanding of what the above question means seethe below code.
    int i =0;
    MessageBox.Show(i.ToString());
    MessageBox.Show(Convert.ToString(i));
    We can convert the integer “i” using “i.ToString()” or “Convert.ToString” so what’s the difference.
    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.
  • Saturday, November 04, 2006 1:33 PMKuju Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi

    There is no difference between i.ToString() and Convert.ToString(i).

    This is the body of the Convert method:

    public static string ToString(int value)
    {
        return value.ToString();
    }

    Furthermore, the integer variable cannot be null since its a value type.

All Replies

  • Friday, November 03, 2006 7:57 PMMuhammad Adnan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Just to give an understanding of what the above question means seethe below code.
    int i =0;
    MessageBox.Show(i.ToString());
    MessageBox.Show(Convert.ToString(i));
    We can convert the integer “i” using “i.ToString()” or “Convert.ToString” so what’s the difference.
    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.
  • Saturday, November 04, 2006 1:33 PMKuju Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi

    There is no difference between i.ToString() and Convert.ToString(i).

    This is the body of the Convert method:

    public static string ToString(int value)
    {
        return value.ToString();
    }

    Furthermore, the integer variable cannot be null since its a value type.

  • Tuesday, November 07, 2006 12:00 PMBhaskar Sharma Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     Kuju wrote:
    Hi

    There is no difference between i.ToString() and Convert.ToString(i).

    This is the body of the Convert method:

    public static string ToString(int value)
    {
        return value.ToString();
    }

    Furthermore, the integer variable cannot be null since its a value type.

    Kuju is absolutely correct.But what will happen if tostring is to be used with an custom object

  • Tuesday, November 07, 2006 9:16 PMKuju Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Once again using the trusty reflector,  the body of the Convert.ToString(object value)looks like this:

    public static string ToString(object value, IFormatProvider provider)
    {
        IConvertible convertible = value as IConvertible
        if(convertible != null)
        {
           return convertible.ToString(provider)
        }
        if(value != null)
        {
           return value.ToString();
        }
        return string.Empty
    }


    Therefore, when you try Convert.ToString() on a custum object, you are guaranteed not to get an ObjectReferenceNullException. On the other hand you might want to get an exception if you don't expect your object to be null.


  • Friday, February 22, 2008 9:43 AMSrini2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    The difference between Convert.ToString and .ToString() as follows

     

    Convert.ToString wont allow null values. Cannot convert null to String

     

    Whereas   .ToString accepts null values

     

    Thanks,

    Srinivasan.K

    Sybrant Technologies Pvt. Ltd.,

     

     

  • Friday, June 13, 2008 2:53 PMfreaky roach Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Srini,

    i think its the other way round
  • Monday, December 14, 2009 3:07 PMPradeep Agrawal Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Offcourse there is a difference:

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

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

  • Friday, January 15, 2010 4:08 PMMurat Yıldız Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Just to give an understanding of what the above question means seethe below code.
    int i =0;
    MessageBox.Show(i.ToString());
    MessageBox.Show(Convert.ToString(i));
    We can convert the integer “i” using “i.ToString()” or “Convert.ToString” so what’s the difference.
    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.

    Muhammed Adnan is absolutely correct, so we use Convert.ToString() much more than ToString()...