C # integers three coercion types convert int, convert.toint32 (), Int. The difference between the Parse ()

Source: Internet
Author: User

1, int is suitable for conversion between simple data types, the default integral type of C # is int32 (bool type is not supported);

2, Int. Parse (String sparameter) is a constructor, and parameter types only support string types;

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

4, Convert.ToInt32 () and Int. The nuances of Parse ():

For null (NULL) processing, CONVERT.TOINT32 (NULL) returns 0 without generating any exceptions, but int. Parse (NULL) produces an exception.

For example:

If we take the value of a parameter page from the URL, we know that the 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, then the former will return 0,0 may be a valid value, so you do not know that the URL is not in the original there is no such parameter and continue the next processing, This can have unintended effects, and in the latter case there is no page this argument throws an exception, we can catch the exception and then do the corresponding processing, such as prompting the user for missing parameters, rather than the parameter value as a limit processing.

5. Another difference is:

(1). Convert.ToInt32 (double value) if value is a number in the middle of two integers, returns the even of the two, that is, 4.5 is converted to 4, and 5.5 is converted to 6;

(2). Int (4.6) = 4,int conversion Other numeric types are int without rounding, casting (intercepting integer part);

(3). Int. Parse (4.5) will directly error: "The input string is malformed."

Int. Parse is to convert a string to int;

Convert.ToInt32 is the conversion of objects that inherit from object to int; You get an object, you want to convert it to int, with Int. It is not possible to parse, but to use Convert.ToInt32.

Summarize:

(1) Convert.ToInt32 parameters are more, Int.parse can only convert string types.

(2) The parse is to convert a string into a int,char,double .... And so on, that is *. Parse (string) must be a string in parentheses.

(3) Convert can provide multiple types of conversions, that is, convert.* () can be of many types (including string) in parentheses.

C # integers three coercion types convert int, convert.toint32 (), Int. The difference between the 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.