C # in (int), int. Parse (), Int. The difference between TryParse () and Convert.ToInt32 ()

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/leolis/p/3968943.html

In the process of programming, data conversion is often used, there are many methods of data conversion in C #, there are four ways to convert the target object to int (int.): (int), int, respectively. Parse (), Int. TryParse () and Convert.ToInt32 (), what are the four methods that limit the objects being converted and what are the differences between them? I believe that many children's shoes can not be completely clear.

Now, from the object being transformed, in our actual development of the project, we encounter the types that need to be converted are about 3 categories, namely null (NULL), numeric type (including Float,double,int,long, etc.), and string 3.

Let's look at the first case: NULL and test with the following code:

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 be in the last sentence error: "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 value type , then comment out the last sentence and run it again to find this sentence (int c = Int. Parse (null);) will report the following error: "Value cannot be null.", value cannot be null, A and B return 0,rlt to false respectively;

Then continue to look at the second case: number type (mainly test double and long type), 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;

Then run it and find this sentence (int c = Int. Parse (M.tostring ());) Error: "Input string is not in a correct format.", the input string is malformed, comment out the sentence and then run, then look at the return value, A=1,b=0,rlt=false , d=1, change the value of M to 1.532d and run it again to see the result as a=2,b=0,rlt=false,d=1; The following tests the long type, modifying the code to:

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 discovery (int a = Convert.ToInt32 (m);) and (int c = int). Parse (M.tostring ());) Error: "Value is either too large or too small for a Int32.", the value is too large or too small for Int32, other return results b=0,rlt=false,d=-1;

The following is the third case: string, the same code to modify the following:

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;

Find the last sentence (int d = (int) m;) Error: "Cannot convert type ' string ' to ' int '", cannot convert string to int type, also comment out this sentence again run, found (int a = Convert.ToInt32 ( m) and (int c = Int.) Parse (m);) reported the following error: "Input string is not in a correct format.", the input string is malformed, and only a string that modifies the value of M to an integer type (for example: "12") will not be reported as such error.

Well, the test is done, and here's a summary:

1) for converted objects, Convert.ToInt32 () can be of various types (example out of a numeric type outside bool,datetime, etc.), Int. TryParse () and Int. Parse () can only be an integer string type (that is, the form after various integer tostring (), cannot be a float, or int. Parse () will appear with an incorrect string format for the input int. TryParse () also returns FALSE, the output parameter is 0, and (int) can only be a numeric type (example Float,int,uint, etc.);

2) for null value NULL, from the point of Operation Error, (int) cast and int. Parse () does not accept that null;convert.toint32 () is actually a judgment before the conversion, if the parameter is NULL, return 0 directly, otherwise call Int. Parse () is converted, int. TryParse () is actually a pair of int. Parse () Does an exception handling, returns False if an exception occurs, and returns the output parameter to 0;

3) for floating-point type of trade-offs, floating-point type only Convert.ToInt32 () and (int) can be converted, but it is also a trade-off, Convert.ToInt32 () take the trade-offs are rounding, and (int) is to intercept the integer portion of the floating-point type, Ignoring fractional portions, such as Convert.ToInt32 (1.499d) and (int) 1.499d, returns 1,convert.toint32 (1.5D) and returns 2, while (int) 1.5d returns 1;

4) for overflow, convert large data types to small data types when Convert.ToInt32 () and Int. Parse () will report an overflow error, the value is too large or too small for Int32, and (int) does not error, but the return value is-1.

So, we choose the conversion method before the data conversion is prudent, if the number type can be considered directly with (int) cast, if it is an integer string type, consider using Int. Parse () to convert, if not both, consider converting with Convert.ToInt32 ().

C # in (int), int. Parse (), Int. The difference between TryParse () and Convert.ToInt32 ()

Related Article

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.