Pages

Sunday, 28 October 2012

Difference between Convert.ToString() and object.ToString()?

Both statement are use to convert into string but the main difference is in handling of NULL value.
Convert.ToString() : It handles NULL reference exception.
.ToString() : It doesn’t handle NULL reference exception.
 So for good programming practice, always use Convert.ToString() for safety.

Code Snippet :
// Throws exception
int salary = null;
string oldSalary = salary.tostring();

// Return NULL value
int salary = null;
string oldSalary = Convert.ToString(salary);

 






No comments:

Post a Comment