string variable int of type conversion

Source: Internet
Author: User

Int.parse () The function is to convert a string type into a int type.

Improper use of int.parse () , may throw an exception, divided into the following three kinds:

    1. System.ArgumentNullException, that is, the contents of the converted string are null;
    2. System.FormatException, the contents of the string are not numbers;
    3. System.OverflowException, the string is not within the range of the int type after the conversion, causing an overflow.

Therefore, using Int.parse () to convert is very easy to restrict, generally if you can determine the content of the converted string, but simply to convert, you can use this method. If the input string is variable, or if you want to judge it first, you can use other conversion methods, the following describes several of the s - t ring into Other functions of int:

1.int. TryParse

Int. TryParse and Int. Parse is similar, but it does not produce an exception because the converted content is not valid, the conversion succeeds, the conversion fails to return FALSE, the last parameter is the output value, and if the conversion fails, the output value is 0. If successful, returns the converted int value.

2.convert.toint32 ()

First, Convert.ToInt32 (), which can be converted to more types, can convert the object class type to an int type, and int. Parse will error if you convert a type other than a number. When using this function, if the converted content is null, 0 is returned, and if "", an exception is thrown; This method is associated with an int. The parse is also more similar, in fact Convert.ToInt32 () internally called Int. Parse.

There are also some small details that, when the number in the middle of the two adjacent integers is returned, returns the even number of the two, or the rounding method if it is not the middle. if Int. Parse has a decimal number and an error: The string you entered is not formatted correctly. If you use the (int) method described below, no rounding will be cast.

3. (int)

(int) is used for simple type conversions in the form of (int) variables, C # default type is Int32, and bool conversions are not supported. is an explicit cast when we can use implicit conversions from the int type to the long, flout, double, and decimal types, but when converting from a long type to an int type, an explicit cast is generated because of the large range of long types, which results in a compilation error.

string variable int of type conversion

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.