Monday, June 17, 2013

C#,Vb.Net - Difference between Convert.tostring(Object) and Object.tostring() method Example

Introduction:

In this Post I will explain you differences between obj.tostring() and Convert.tostring(obj) in asp.net.

Explanation :

The basic difference between them is “Convert.ToString(Object)” handles NULL values even if variable value become null but “Object.ToString()” will not handle NULL values it will throw a NULL reference exception error. So as a good coding practice using “convert” is always safe.

Example:
//Returns an empty string for str and does not throw an exception. string str; object i = null; str = Convert.ToString(i); Returns a null reference exception for str. string str; object i = null; str = i.ToString();

No comments:

Post a Comment