Int. Parse () and Int. TryParse ()

Source: Internet
Author: User

Source:http://www.cnblogs.com/fishtreeyu/archive/2011/01/15/1936193.html

int i =-1;
BOOL B = Int. TryParse (null, out i);

When execution is complete, b equals false,i equals 0, not equal to-1, remember.

int i =-1;
BOOL B = Int. TryParse ("123", out I);

After execution, b equals true,i equals 123;


1, (int) is a type conversion; when we 觟 the NT 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 another type of parameter;

Comparison: When the Convert.ToInt32 parameter is null, returns 0; Int. Throws an exception when the Parse parameter is null. Throws an exception when the Convert.ToInt32 parameter is ""; Int.   Throws an exception when the Parse parameter is "". Convert.ToInt32 can convert more types; int. Parse can only convert strings of numeric types

Int. Parse () and Int. TryParse ()

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.