About Int. Exception to Parse ()

Source: Internet
Author: User

one, Int. Parse (string variable name)

This is the way to convert a string of numeric content to an int type

1. If the contents of the string are null, the ArgumentNullException exception is thrown;

2. If the string content is not a number, the FormatException exception is thrown;

3. Use this method to handle only the contents of the string, and the converted string content is to be within the range of the int type, and if exceeded, throws a OverflowException exception.

Second, on-line search related content, Convert.ToInt32 (), Int. Parse (), Int. TryParse (), (int) These functions can convert other types to int type

1.convert.toint32 () and Int. The difference between Parse ():

(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 .

(2) When the parameter is a floating-point number:

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 "

(3) Different conversion objects:

A.int.parse is the conversion of string int
B.convert.toint32 is the conversion of objects that inherit from object to int

When you get an object, you want to convert it to int, with Int. It is not possible to parse, but to use Convert.ToInt32.

2.int. TryParse () and Int. Parse ():

Int. TryParse () and Int. Parse () is more similar, but it does not produce an exception, the conversion succeeds 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

3. About (int):

(int) is a type conversion; 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.

Third, this is the code found on the Internet, can be used as a future contact material:

classprogram{Static voidMain (string[] args) {        stringstr ="1,2,3,4,5,"; Console.WriteLine (str. Substring (0Str. Length-1)); intNums =0; stringnum =NULL; Int32.TryParse (num, outnums);         Console.WriteLine (Nums); /*int nums = 0;        String num = "12345";        Int32.TryParse (num, out nums);         Console.WriteLine (Nums); */Nums=Convert.ToInt32 (num); Nums=int.    Parse (num); }

Okay ~ See you next week ~

About Int. Exception to 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.