I will explain you what is the different between .ToString() and Convert.ToString() with the help of below mentioned code:
int intAmit =0;
Response.write(intAmit.ToString());
Response.write(Convert.ToString(intAmit));
We can convert the integer “intAmit” using “intAmit.ToString()” or “Convert.ToString(intAmit)” ,so what is the basic difference between them:
Convert: function handles NULLS
.ToString(): does not handles NULLS and it will throw a NULL reference exception error.
So as good coding practice is to use “convert” and it is always safe.
Example:
For example you received the data from database in the DataTable. One of the column in datatable is "Amit" and "Amit" column value in some rows of datatable are NULL.
So, when you will try to convert "Amit" column value to string if column value is NULL, with the help of dt.Row[0]["Amit"].ToString() it will throw a NULL reference exception error.
According to me when you need to convert an object to a string and it may be null, you can do it without throwing an exception using Convert.ToString().
int intAmit =0;
Response.write(intAmit.ToString());
Response.write(Convert.ToString(intAmit));
We can convert the integer “intAmit” using “intAmit.ToString()” or “Convert.ToString(intAmit)” ,so what is the basic difference between them:
Convert: function handles NULLS
.ToString(): does not handles NULLS and it will throw a NULL reference exception error.
So as good coding practice is to use “convert” and it is always safe.
Example:
For example you received the data from database in the DataTable. One of the column in datatable is "Amit" and "Amit" column value in some rows of datatable are NULL.
So, when you will try to convert "Amit" column value to string if column value is NULL, with the help of dt.Row[0]["Amit"].ToString() it will throw a NULL reference exception error.
According to me when you need to convert an object to a string and it may be null, you can do it without throwing an exception using Convert.ToString().