. Net Data Type conversions

Source: Internet
Author: User

Implicitly-Typed conversions:

Small range to large range;

Char to other types is possible, and vice versa.

Display Type conversions:

Simple Type

    1. char a = (char) 1;   Simple type of conversion, compile times wrong. (4.6 to 4, straight to decimal)
    2. int b = Convert.ToInt32 (123.4);   Simple type of conversion, execution times wrong. (4.6 is 5, rounded, decimal is 5 o'clock, return even)
    3. int c = Int32.Parse ("123");   Only string to number, execution times wrong. ("4.6" simultaneous failed with format error)

2 and 3 differences: The result of Convert.ToInt32 (null) is 0,int32.parse (null) ArgumentNullException exception.

Reference type

Key words Data type Parameter relationships Run-time failure
CType Any data type Widening or narrowing conversions must be defined between two types of data Trigger InvalidCastException
DirectCast Any data type A type must inherit from or implement another type Trigger InvalidCastException
TryCast Reference type only A type must inherit from or implement another type Return Nothing

Dim p as Product
p = CType (obj, Product)
p = DirectCast (obj, Product)

Example: c = DirectCast (f, System.Windows.Forms.Control) The best efficiency
P =trycast (obj, Product)

. Net Data Type conversions

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.