In C #, under what conditions is int used and convert. toint32 used?

Source: Internet
Author: User

Baidu question:
For example, there is a string type 3
To convert it to the int type, use (INT) 3.
Or use convert. toint32 (3 );
Or can both be used. Why?

Best Answer:

First of all, I want to point out that in C #, Int Is Actually system. int32, that is, all are 32-bit.

Second, (INT) and convert. toint32 are two different concepts. The former is type conversion, while the latter is content conversion. They are not always equivalent. We know that C # provides a type check. If you cannot forcibly convert a string to an int, implicit conversion is more impossible. For exampleCodeIt won't work:

String text = "1412 ";
Int id = (INT) text;

Because string and INT are two completely different and incompatible types. Speaking of this, you may ask what is compatible? In fact, you can only use (INT) for forced conversions of numerical types, such as long, short, and double. However, you need to consider the accuracy when performing such conversions.

However, we know that text in the above Code actually stores a value. We hope to extract the value and store it in the form of an int for future computation, then you need to convert the content. Content conversion is also called content explanation. We can modify the above Code to achieve the goal:

String text = "1412 ";
Int id = convert. toint32 (text );

In addition, you can use int32.parse and int32.tryparse for explanation.

In addition, you find convert. toint32 has many overloaded versions, such as convert. toint32 (double value); when we use this version to convert a double to an int, toint32 checks whether the converted value can be expressed in Int, that is, whether "out-of-bounds" occurs. If yes, overflowexception will be thrown. Otherwise, the conversion will be performed for you, but the (INT) is used for forced conversion. If the value to be converted is greater than int32.maxvalue, then you will get an incorrect result, such as the following code:

Double D = int32.maxvalues + 0.1412;
Int I = (INT) D;

However, no matter what value conversion you perform, the accuracy issue must be considered.

Related Article

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.