Wednesday, December 3, 2008

Difference Between Convert.ToString() and .ToString()

string stringVariable = null;
string stringHolder = stringVariable.ToString();
While in runtime suppose “stringVariable” happens to get “null” assigned we get “Object reference not set to an instance of an object.” exception.


string stringVariable = null;
string stringHolder = Convert.ToString(stringVariable);
While using the above snippet, even if “stringVariable” gets assigned “null” Convert.ToString() with return “”(an empty string) by avoiding exception.

So, better to use Convert.ToString() instead of .ToString()

1 comments:

Kannan said...

Hi,

Its good information Boy.

Keep it up!!