Possible Duplicate:
variable.ToString() vs. Convert.ToString(variable)
What is the difference between Convert.ToString() and .ToString() in C#?
When I try and convert dataRow[i]
to a string using ToString() then I receive an error. How do I fix this?
Both these methods are used to convert a value to a string. The difference is Convert. ToString() method handles null whereas the ToString() doesn't handle null in C#. In C# if you declare a string variable and if you don't assign any value to that variable, then by default that variable takes a null value.
But yes, there is a difference between them and the main difference is that Convert. Tostring() function handles the NULL while the . ToString() method does not and it throws a NULL reference exception. So, it is a good programming practice to use Convert.
The toString() method can be used to convert a string object into a string.
ToString(Int32, IFormatProvider) Converts the value of the specified 32-bit signed integer to its equivalent string representation, using the specified culture-specific formatting information.
Basically both are used to convert a value to a String but there is a basic difference between them:
When we have an NULL object, Convert.ToString(Object);
handles the NULL value whereas Object.ToString();
does not handle the NULL value and it throws NULL Reference Exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With