Int. Parse () and its anomaly judgment

Source: Internet
Author: User

1, Int. Parse () is a class-tolerant conversion that represents a string of numeric content to 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;

2, except Int. Parse (), C # CNOOC Other functions can convert a type to an int type. such as Convert.ToInt32 (), Int. Functions such as TryParse (), (int).

    • 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, and if the conversion succeeds, the output value is the converted int value.

    • 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

Convert.ToInt32 and Int. The Parse is more similar, in fact Convert.ToInt32 internally called Int. Parse:

      • Returns 0;int when the Convert.ToInt32 parameter is null. 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 a string of numeric types.
    • (int) is a cast conversion, and when we extend the int type to the Long,float,double,decimal type, we can use implicit conversions, but we need to use an explicit conversion from the long type to the int type, otherwise a compilation error is generated. But we can only convert other numeric types to int type, it cannot convert strings,

For example, the following example will fail:
String v = "1";
int n = (int) v;

3, test code from a class & quiet

1 class Program 2 {3     static void Main (string[] args) 4     {5         string str = "1,2,3,4,5,"; 6         Console.WriteLine (str. Substring (0, str. LENGTH-1)); 7   8         int nums = 0; 9         string num = null;10         int32.tryparse (num, out nums);         Console.WriteLine ( nums);         /*14         int nums = 0;15         string num = "12345";         int32.tryparse (num, out nums); 17         Console.WriteLine (nums),          */19         nums = Convert.ToInt32 (num),         nums = Int. Parse (num);     

Int. Parse () and its anomaly judgment

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.