Purushottam Rathore
What is the difference between Convert.toString and .toString() method ?
By Purushottam Rathore in ASP.NET on Aug 12 2009
  • Puran Mehra
    Aug, 2009 13

    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.

    • 0
  • Maruti Tathe
    Aug, 2009 13

    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;



    • 0
  • Purushottam Rathore
    Aug, 2009 12

    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.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS