(Int), Int32.Parse () and Convert. toInt32 ()

Source: Internet
Author: User
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 numerical values must be considered.

 

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 numerical values must be considered.

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.