Software Testing week fourth--on the type conversion of Int.parse ()

Source: Internet
Author: User

Let's start by summarizing all the types of conversions we've used:

1. Implicit type conversion, that is, using (int) to force type conversions directly. The advantage of this approach is that it is simply rude, specifying the conversion type directly, without any protection, so it is also easy to throw an exception that causes the program to crash. When we convert the int type to the Long,float,double,decimal type, we can use implicit conversions, but when we need to use an explicit conversion from the long type to the int type, a compilation error is generated.

2.int. Parse () is a class-tolerant transformation that converts a string of numeric content into an int type.

Throws a ArgumentNullException exception if the string is empty;

Throws a FormatException exception if the string content is not a number;

Throws a OverflowException exception if the string content represents a number that is outside the range represented by the int type;

  

3.int. TryParse and Int. Parse is more similar, but it does not produce an exception, the conversion succeeds and returns True, and the conversion fails to return false.

The last parameter is the output value, if the conversion fails, the output value is 0, if the conversion succeeds, the output value is the converted int value

  

4.convert.toint32 () is a class-tolerant conversion, but it is not limited to converting a string to an int type or to another type of parameter;

    

(1) The biggest difference between the two methods is their handling of the null value:
Convert.ToInt32 (NULL) returns 0 without generating any exceptions, but int. Parse (NULL) produces an exception.

Do not understand Chu Convert.ToInt32 and int.parse () fine nuances, do not mess with, otherwise it may produce unpredictable results, for example: If you take a parameter page from the URL value, we know that this value is an int, the can be used Convert.ToInt32 (request.querystring["page"]), can also be used, 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 at all in this 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 to lack of parameters, rather than the parameter value as a limit processing.

(2) There's a little difference.
A. Convert.ToInt32 (double value)
If value is a number in the middle of two integers, the even numbers are returned, that is, 3.5 is converted to 4, 4.5 is converted to 4, and 5.5 is converted to 6. But 4.6 can be converted to 5, 4.4 to 4
B.int.parse ("4.5")
Direct error: "The input string is not properly formatted ."

c. int (4.6) = 4
int conversions Other numeric types are int when not rounded, cast

Int. Parse is converting 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.

    

Software Testing week fourth--on the type conversion of Int.parse ()

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.