Hi
Please explain the Difference between Convert Vs String.ToString
Hi
Please explain the Difference between Convert Vs String.ToString
Last edited by jbanx; 02-22-2007 at 02:12 AM.
Hi,
Diffrence between string.convert vs string.tostring is here tostring doesnot handle null values where as covert handle null values.
Regards,
hari prasad
To tell you more Convert internally uses toString. In convert first there is a check for null, if there is null then string.empty is returned, else normal toString method is called
String.Convert:
convert the string from one to another.
String.toString:
divide the string into sub string
String.Convert will not allow null value
String.ToString will allow null value...
See the example.
Line 1 :String str1 = null;
Line 2: Console.WriteLine("->" + Convert.ToString(str1) + "<-";
Line 3: Console.WriteLine("->" + str1.ToString() + "<-");
In the above example,
Line 2 will display the result("-><-"), because Convert.ToString() handle NULL.
Line 3 will throw NullReferenceException because .ToString() does not handle NULL.