Follow in Twitter & Facebook


Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools



Friday, October 26, 2012

Difference between convert.tostring() and .ToString() in C#.Net









In this article we will discuss what is the difference between convert.tostring() and .tostring() in C#.Net. You can also check my previous articles on how to split a string using javascript, how to remove html tags from string in C#.Net and What is difference between String and StringBuilder in C#.Net.

The major difference between convert.tostring() and .tostring() is Convert.ToString() handles null value but ToString() doesn't handle null value, means whenever you will try to convert null to string using .ToString it will through an Null reference exception.

.ToString() raise exception when the object is null
Convert.ToString() return string.Empty in case of null object

Example

int age=0;
string strAge= Convert.ToString(age); //This will not throw any execption and will convert it to string.

string strAge= age.ToString(); //This will not handle null value and eill throw null reference exception.




1 on: "Difference between convert.tostring() and .ToString() in C#.Net"
  1. Even my project is of C#.net i have faced problem while i am involved in to the sting part!Thanks for this as i want to preserve the data!
    Questions

    ReplyDelete