In previous projects, I seldom paid attention to convert, parse, tryparse, Int, and other differences. I found that these details were very important only recently, and I felt that convert, parse, tryparse, and INT were the best way to use them;
Convert. toint32 is similar to int. parse. Actually, convert. toint32 calls Int. parse:
* If the convert. toint32 parameter is null, 0 is returned;
* If the Int. parse parameter is null, an exception is thrown.
*
* An exception is thrown when the convert. toint32 parameter is;
* If the Int. parse parameter is "", an exception is thrown.
*
* Convert. toint32 can be converted to many types;
* Int. parse can only convert numeric strings.
Difference between convert. toint32 () and Int. parse () (1) the biggest difference between the two methods is their processing method for null values:
Convert. toint32 (null) returns 0 without any exception, but Int. parse (null) returns an exception.
Not clear about convert. toint32 and Int. do not use the details of parse () unless otherwise unexpected results are generated. For example, if you take the value of the page parameter from the URL, we know that this value is an int, so you can use convert. toint32 (request. querystring ["page"]), or Int. parse (request. querystring ["page"]), but if the page parameter does not exist in the URL, the former returns 0, 0, which may be a valid value, therefore, if you do not know that the URL does not have this parameter, you can continue to process it. This may produce unexpected results, in the latter way, if no page parameter is used, an exception will be thrown. We can capture the exception and then handle it accordingly. For example, if you are prompted that a parameter is missing, instead of processing the parameter value as 0.
(2) Another difference is that
A. Convert. toint32 (double value)
If value is a number between two integers, an even number is returned. That is, 3.5 is converted to 4, 4.5 is converted to 4, and 5.5 is converted to 6. However, 4.6 can be converted to 5, and 4.4 to 4.
B .int. parse ("4.5 ")
Error: "The format of the input string is incorrect ".
C. INT (4.6) = 4
When int is converted to another numeric type, it is not rounded to the nearest integer. Forced conversion
Int. parse: converts string to int.
Convert. toint32 is used to convert the object inherited from the object to int.
You get an object. If you want to convert it to int, you cannot use Int. parse. You must use convert. toint32.
Personal summary:
(1) convert. toint32 has many parameters. Int. parse can only be converted to string type.
(2) Parse converts string to int, Char, double..., and so on, that is, *. parse (string) must be string.
(3) convert can provide conversion of multiple types, that is, convert. * () Many types (including string) can be considered in parentheses ).
Int. tryparse is similar to int. parse, but it does not produce exceptions. If the conversion is successful, true is returned. If the conversion fails, false is returned. The last parameter is the output value. If the conversion fails, the output value is 0.
Int m;
If (Int. tryparse ("2"), Out m)
{
...
}
Returns true, runs in {}, and assigns m a value of 2;
If (Int. tryparse ("DDD"), Out m)
{
...
}
Returns false. If {} is not run, M is assigned 0;
(INT) is cast conversion. It 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;