In C #, what are the differences between (INT), int32.parse (), convert. toint32 (), and int32.tryparse?

Source: Internet
Author: User

In C #, what are the differences between (INT), int32.parse (), convert. toint32 (), and int32.tryparse?

The Int keyword indicates an integer that is 32-bit. Its. NET Framework type is system. int32.
If int32.tryparse () does not throw an exception, true or false is returned to indicate whether the resolution is successful. If a parsing error occurs, the caller will get a value of 0.
(INT) indicates explicit forced conversion, which is a type conversion. Implicit conversions can be used when we convert from the int type to the long, float, double, or decimal type, but when we convert from the long type to the int type, we need

Use explicit forced conversion; 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].

××××××××××××××××××××××××××××××××××××××××××× *

Convert. toint32, (INT), and Int. parse:

The former is suitable for converting the object class type to the int type, such as convert. toint32 (session ["shuzi"]);

(INT) converts between simple data types;

Int. parse is suitable for converting string type to int type, such as Int. parse (session ["shuzi"]. tostring ()).
 

(1) The biggest difference between the two methods is their processing method for null values:
Convert. toint32 (null) returns 0 without any exception, but Int. parse (null) returns an exception.

Not clear about convert. toint32 and Int. do not use the details of parse () unless otherwise unexpected results are generated. For example, if you take the value of the page parameter from the URL, we know that this value is an int, so we can

Use convert. toint32 (request. querystring ["page"]), or Int. parse (request. querystring ["page"]), but if the page parameter does not exist in the URL, the former returns 0, 0, which may be a valid value.

So you do not know that the URL does not have this parameter at all and continue to process it. This may produce unexpected results, in the latter way, if the page parameter is not used, an exception is thrown. We can capture the exception and then perform the corresponding operation.

For example, if you are prompted that a parameter is missing, instead of processing the parameter value as 0.

(2) Another difference is that
A. Convert. toint32 (double value)
If value is a number between two integers, an even number is returned. That is, 3.5 is converted to 4, 4.5 is converted to 4, and 5.5 is converted to 6. However, 4.6 can be converted to 5, and 4.4 to 4.
B .int. parse ("4.5 ")
Error: "The format of the input string is incorrect ".

C. INT (4.6) = 4
When int is converted to another numeric type, it is not rounded to the nearest integer. Forced conversion

Int. parse: converts string to int.
Convert. toint32 is used to convert the object inherited from the object to int.
You get an object. If you want to convert it to int, you cannot use Int. parse. You must use convert. toint32.

personal summary:
(1) convert. toint32 has many parameters, Int. parse can only be converted to string type.
(2) Parse converts string to int, Char, double ...., that is *. parse (string) must be string.
(3) convert can provide multiple types of conversion, that is, convert. * () There are many types (including string) in parentheses ).
(4) the difference in efficiency can be found here Article : elaborate on the efficiency of convert and parse

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.