Tostring = Convert.
Tostring = (
String ) Normally
String There are four methods: (assume there are object OBJ) obj.
Tostring , Convert.
Tostring ,(
String ) OBJ, OBJ
String .
They can all convert object objects
String Object. Let me talk about their similarities and differences and what they should be used in practice.
The first two methods are usually obtained by other objects.StringObjects. The differences between them are only manifested in the case that the object to be converted is null. If obj is null, obj is called.TostringMethod causes nullreferenceexception and calls convert.TostringReturns NULL if no exception is thrown.
Use force conversion (String) OBJ requires that the runtime type of OBJ must beString. If not, an exception is thrown. The AS method is relatively stable, and the runtime type of obj is notStringWill return NULL without throwing an exception.
so when we need to obtain the string Expression of an object, we should use tostring and convert. tostring , if you can ensure that your object is not null, the two are similar. If it may be null, you should use convert. tostring . If you want to throw an exception when it is null, you can select. tostring .