In C #, what are the differences between (INT), int32.parse () and convert. toint32?
The Int keyword indicates an integer that is 32-bit. Its. NET Framework type is system. int32.
(INT) indicates explicit forced conversion, which is a type conversion. Implicit conversions can be used when we convert from int type to long, float, double, or decimal type. However, explicit forced conversions are required when we convert from long type to int type, otherwise, a compilation error occurs.
Int32.parse () indicates to convert the string of a number to a 32-bit signed integer, which belongs to content conversion [1].
A common method is public static int parse (string ).
If the string is null, an argumentnullexception exception is thrown;
If the string format is incorrect, A formatexception is thrown;
If the string value is smaller than minvalue or a number greater than maxvalue, an overflowexception exception is thrown.
Convert. toint32 () can convert values of multiple types (including object reference types) to int type, because it has many overloaded versions [2]:
Public static int toint32 (object );
Public static int toint32 (bool );
Public static int toint32 (byte );
Public static int toint32 (char );
Public static int toint32 (decimal );
Public static int toint32 (double );
Public static int toint32 (short );
Public static int toint32 (long );
Public static int toint32 (sbyte );
Public static int toint32 (string );
......
The applications of (INT), int32.parse (), and convert. toint32 () are as follows:
Example 1:
Long longtypes = 100;
Int inttype = longtype; // error. Explicit forced conversion is required.
Int inttype = (INT) longtype; // The value is correct. An explicit forced conversion is used.
Example 2:
String stringtype = "12345 ";
Int inttype = (INT) stringtype; // error. The string type cannot be directly converted to the int type.
Int inttype = int32.parse (stringtype); // correct
Example 3:
Long longtypes = 100;
String stringtype = "12345 ";
Objecttype = "54321 ";
Int inttype = convert. toint32 (longtype); // correct
Int inttype = convert. toint32 (stringtype); // correct
Int inttype = convert. toint32 (objecttype); // correct
Example 4 [1]:
Double doubletype = int32.maxvalue + 1.011;
Int inttype = (INT) doubletype; // although the operation is correct, an error is returned.
Int inttype = convert. toint32 (doubletype) // throw an overflowexception exception
The differences between (INT), int32.parse (), and convert. toint32 () are as follows:
The first one is used for explicit forced conversions from the long type or floating point type to the int type. However, if the converted value is greater than int32.maxvalue or less than int32.minvalue, an error is returned.
The second one is used during the conversion from string to int type that conform to the numeric format, and an exception can be thrown to the wrong string numeric format.
Third, you can convert multiple types of values to the int type, or throw an exception to the wrong value.
Regardless of the type of numerical conversion, the accuracy of the numerical value is an issue we must consider [1].