3
Reply

What is the difference between Convert.toString and .toString() method ?

    What is the difference between Convert.toString and. ToString() method ?

     

    Convert.toString uses convert class method, which contains different static method to convert one primitive type to another primitive type.

     

    While The ToString method of the Object class converts a type to a string type and it has many overloads.

    Hi,


    This is very common interview question; been discussed many times. 
    1) .toString() will not handle NULL; in case of NULL values it will raise NullReferenceException
    2) Convert.toString() will handle NULL;



    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.