Int.parse ("abc") 's Fault

Source: Internet
Author: User

Int.parse () is a forced reload function.

Int. Parse (String type variable name)

The method is to convert a string of numeric content to an int type, or to throw a ArgumentNullException exception if the string content is empty or null, or to throw a FormatException exception if the string content is not a number The OverflowException exception is thrown if the string content represents a number that is outside the range that the int type can represent.

One of the things you can do with this approach is to handle only string content, and the string content can only be within the range represented by the int type.

The formatexception exception occurs because the ABC string is not a number.

Through the online query forced reload function and usage. I found 3 functions. Convert , Parse, TryParse, (int) functions are all converting values to integers, but there are similarities and differences between the four

1 (int) variable name [coercion type conversion]:

This conversion is primarily used for numeric type conversions, from int to long,float,double,decimal types, and implicit conversions are possible, but the use of a long type to an int type requires an explicit conversion, which is how the data type is converted, or a compilation error occurs.

The method is unconditional for floating-point numbers, losing precision.

2int. TryParse (string s, out int result)

This is also the way to convert a string of numeric content to an int type, but that way is more than int. The good thing about parse is that it does not appear to be abnormal. If the conversion succeeds returns True, False is returned if the conversion fails. Obviously, the last parameter is the output value, if the conversion fails, the output value is 0, and if the conversion succeeds, the corresponding value is output.

4 Convert.ToInt32

This method not only converts a string to an int type, but also converts the value of another type to an int type. If the variable is an object or string type, when its value is null, it returns 0, without causing a program error, but if the value of this string type is string.     Empty, which can still cause a program error when transitioning to int. This method rounds the floating-point number.

Int.parse ("abc") 's Fault

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.