Analysis. Four methods of net type conversion

Source: Internet
Author: User

. NET type transformation four kinds of practices:

Forced transformation: (int) variable name

Int. Parse (string variable name)

Convert.to type (variable name)

TryParse

Compulsory transition (casting)

Floating point numbers do unconditional, lose precision

Can handle an object to int, but an error occurs when an object is null

In some cases, such as object transformation into an int, sometimes normal, but sometimes for no reason error, unexplained

Unable to process string int (functions often used in the program)

Never be used to handle char types, otherwise the ASCII code is returned instead of the literal number

Int. Parse (string variable name)

can only handle string int or other types, cannot handle types other than string, limited functionality

String was encountered as null or string. Empty will still cause the error of the program

can also change the use of: "Type." Parse (String), but parameters can only be dropped string

Convert.to type (variable name)

can handle all. NET types, functional integrity

can handle NULL, almost no program error due to transformation

When transformed into int by "convert.toint32 (variable), the variable is of type object or string, and when its value is null, it returns 0, without causing the program error

Last, if this string type has a variable whose original value is string. Empty, when converted to int, still cause program error

Floating-point numbers are rounded.

Never be used to handle char types, otherwise the ASCII code is returned instead of the literal number

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.