Tuesday, July 24, 2012

convert.tostring vs tostring in c#


int someValue = 4;
// You can do this
txtSomeValue.Text = someValue.ToString();
// Or this...
txtSomeValue.Text = Convert.ToString(someValue);

int x =0; 
string s=x.ToString(); 
string Str=Convert.ToString(x); 

Here We can convert the integer “x” using “x.ToString()” or “Convert.ToString()” . But only The basic difference between them is “Convert” function handles NULLS while “x.ToString()” 
does not it will throw a NULL reference exception error. So as for good programming practice using “Convert” is always safe. 

No comments :

Post a Comment