Differences between (INT), Int. parse (), Int. tryparse () and convert. toint32 () in C #

Source: Internet
Author: User

Data conversion is often used in programming. There are many data conversion methods in C #. There are four methods to convert the target object to an integer (INT: they are (INT) and Int. parse (), Int. tryparse () and convert. toint32 (), What are the limitations of the four methods on the converted objects and their differences? I believe that many children's shoes cannot be completely clarified.

Next, let's start with the converted object. In our actual development project process, there are about three types that need to be converted: null), numeric (including float, double, Int, long, etc.) and string (string.

First look at the first case: NULL, use the following code for testing:

Int A = convert. toint32 (null );
Int B;
Bool rlt = int. tryparse (null, out B );
Int c = int. parse (null );
Int d = (INT) NULL;

Obviously, before running, Vs will report an error in the last sentence: "cannot convert null to 'int' because it is a non-nullable value type ", this means that null cannot be converted to int. Because int is a non-null type, comment out the last sentence, run it again, and find this sentence (INT c = int. parse (null);) returns the following error: "value cannot be null. ", the value cannot be blank, 0 is returned for A and B respectively, and RLT is false;

Next, let's look at the second case: Number Type (mainly test the double and long types). First, modify the Code as follows:

Double M = 1.232d;
Int A = convert. toint32 (m );
Int B;
Bool rlt = int. tryparse (M. tostring (), Out B );
Int c = int. parse (M. tostring ());
Int d = (INT) m;

Run the command and find this sentence (INT c = int. parse (M. tostring ();), the following error occurs: "input string was not in a correct format. ", the input string format is incorrect. Comment out this sentence and run it. Then, check the return value, a = 1, B = 0, rlt = false, D = 1, modify the m value to 1.532d and run it again. The result is a = 2, B = 0, rlt = false, D = 1. The following tests the long type, modify the code:

Long M = 9223372036854775807;
Int A = convert. toint32 (m );
Int B;
Bool rlt = int. tryparse (M. tostring (), Out B );
Int c = int. parse (M. tostring ());
Int d = (INT) m;

After running, it is found that (int A = convert. toint32 (m);) and (INT c = int. parse (M. tostring ();) error: "value was either too large or too small for an int32.", the value is too large or too small for int32, other returned results B = 0, rlt = false, D =-1;

Next we will continue to look at the third case: string, the same modification code is as follows:

String M = "1.32 ";
Int A = convert. toint32 (m );
Int B;
Bool rlt = int. tryparse (M, out B );
Int c = int. parse (m );
Int d = (INT) m;

If the last sentence (INT d = (INT) m;) is found, the error "cannot convert type 'string' to 'int'" cannot be converted to the int type, comment out the sentence and run it again. We found that (int A = convert. toint32 (m);) and (INT c = int. parse (m);) All reports the following error: "input string was not in a correct format. ", the input string format is incorrect. This error is not reported only when the m value is modified to an integer string (for example," 12.

Now, the test is complete, and the following is a summary:

1) convert. toint32 () can be of multiple types (for example, bool, datetime, etc.), Int. tryparse () and Int. parse () can only be an integer string type (that is, a variety of integer tostring () form, not a floating point type, otherwise Int. parse (), the input string format is incorrect, Int. tryparse () also returns false, and the output parameter is 0). (INT) can only be numeric (for example, float, Int, uint, etc );

2) For null values, the (INT) force conversion and Int. parse () cannot accept NULL; convert. toint32 () is actually a judgment before conversion. If the parameter is null, 0 is returned directly; otherwise, int is called. parse () for conversion, Int. tryparse () is actually an int. parse () performs Exception Processing. If an exception occurs, false is returned, and 0 is returned for the output parameter;

3) for the floating point model, only convert is supported. toint32 () and (INT) can be converted, but it is also a trade-off, convert. toint32 () adopts the rounding function, while (INT) truncates the floating point integer part, ignoring the fractional part, such as convert. toint32 (1.499d) and (INT) 1.499d both return 1, convert. toint32 (1.5D) returns 2, while (INT) 1.5D returns 1;

4) for overflow, convert is used to convert a large data type to a small data type. toint32 () and Int. an overflow error is reported for parse (). The value of int32 is too large or too small, while (INT) does not report an error, but the returned value is-1.

Therefore, we should be cautious when selecting the conversion method before data conversion. If it is a numeric type, we can consider using (INT) to force conversion. If it is an integer string type, consider using Int. parse () for conversion. If not, use convert. toint32.

Differences between (INT), Int. parse (), Int. tryparse () and convert. toint32 () in C #

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.