Convert: C # INTEGER: The difference between int, Convert. ToInt32 (), int. Parse,

Source: Internet
Author: User

Convert: C # INTEGER: The difference between int, Convert. ToInt32 (), int. Parse,

1. int is suitable for conversion between simple data types. The default Integer type of C # Is int32 (bool type is not supported );

2. int. Parse (string sParameter) is a constructor. The parameter type only supports the string type;

3. Convert. ToInt32 () is suitable for converting the Object type to the int type;

4. Differences between Convert. ToInt32 () and int. Parse:

The processing of null values is different. Convert. ToInt32 (null) returns 0 without any exception, but int. Parse (null) produces an exception.

For example:

For example, if you get the value of a page parameter from the url, we know that the value is an int, so you 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, therefore, you do not know that the url does not have this parameter at all and will proceed with the next step, which may produce unexpected results; if the latter method is used without the page parameter, an exception is thrown. We can capture the exception and perform corresponding processing, for example, prompting the user to lack a parameter, instead of processing the parameter value as 0.

5. Another difference is:

(1). Convert. ToInt32 (double value) If value is a number between two integers, an even number is returned. That is, 4.5 is converted to 4, while 5.5 is converted to 6;

(2). int (4.6) = 4. If int is converted to another numeric type, it is not rounded off. Forced conversion (truncation of the integer part );

(3). int. Parse (4.5) will directly report an error: "The input string format is incorrect ".

Int. Parse is to convert String to int;

Convert. ToInt32 is used to Convert the Object inherited from the object to the int type. If you get an Object and want to Convert it to the int type, you cannot use int. Parse. You must use Convert. ToInt32.

Summary:

(1) Convert. ToInt32 has many parameters. Int. Parse can only be converted to string type.

(2) Parse converts String to int, char, double..., and so on, that is, *. Parse (string) must be string.

(3) Convert can provide conversion of multiple types, that is, Convert. * () Many types (including string) can be considered in brackets ).

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.