Several Methods to convert string

Source: Internet
Author: User
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 .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.