Comparison of various types of conversions

Source: Internet
Author: User

Notes about type conversion

() Forced conversion Operator

Features:
1. If the type conversion fails, an invalidcastexception will be thrown.
2. As long as the type conversion is true, null does not throw an exception.

Convert type conversion

Features:
1. null values will not throw an exception. For example, convert. tostring (null) will return string. Empty
2. formatexception may be thrown due to incorrect format

As type conversion Operator

Features:
1. Never throw an exception
2. If the input value is null or the conversion fails, null is returned.

Other memos:

The typeof () operator returns the type of a class.
The is operator can determine whether the conversion is successful before forced conversion.
Tostring () function. Because tostring is defined in the object, it can be called for any type, which is convenient. But a little annoying,
The instance of the class cannot be null. Otherwise, an exception is thrown.
Dbnull value, which will be read from the database. This value can be safely called tostring () or convert. tostring, and will return string. Empty
It should be noted that when other functions of the convert class are used, an exception will be thrown when this value is encountered, for example: Convert. todouble (convert. dbnull)

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.