object o;
Is there any difference between o.ToString()
and (string) o
?
Both these methods are used to convert a string. 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.
The toString() method of a string object returns a string representing the specified string.
A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.
String. valueOf will transform a given object that is null to the String "null" , whereas . toString() will throw a NullPointerException . The compiler will use String.
If you're after safe conversion from object to string just use:
string s = Convert.ToString(o);
object.ToString()
will convert the object into a string. If object has null value then it will throw an exception because no null value has ToString()
method.
Whereas (string)object
is a unboxing process of reference type to value type.
Here an object
value is copying into new instance of string type.
If that object
is null, it will assign null value.
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