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.