Conversion of convert. toint32, (INT), and Int. parse

Source: Internet
Author: User

From http://www.cnblogs.com/Ihaveadream/

"

Convert. toint32, (INT), and Int. parse:

The former is suitableObjectClass type conversionIntType, such as convert. toint32 (session ["shuzi"]);

(INT) ApplicableSimple data typeBetween;

Int. parseStringClass type conversionIntType, such as Int. parse (session ["shuzi"]. tostring ()).

 

 

(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 ") 
Direct 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 is to convert string to int
convert. toint32 is the int object inherited from the object.
you get an object, you want to convert it to int and use Int. parse is not allowed. Convert is used. toint32.

my 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 ).
(4) Check the difference in efficiency.Article: Elaborate on the efficiency of convert and parse in the end

"

Baidu Knows

"

 
Convert. toint32, Int. parse (int32.parse), Int. tryparse, and (INT) can all be interpreted as converting data types to int. What are the differences between them? Convert. toint32 and Int. parse is similar. In fact, convert. toint32 internally calls Int. parse: convert. if toint32 is null, 0; int is returned. if the parse parameter is null, an exception is thrown. If the convert. toint32 parameter is "", an exception is thrown. If the Int. parse parameter is "", an exception is thrown. Convert. toint32 can be converted to many types; Int. parse can only be converted to numeric strings. 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.

"

The efficiency of convert and parse is high!

From http://www.cnblogs.com/Ihaveadream/

"

Preface:
A lot of experts are ugly.
We all know that there is a method for us to use during type conversion, that is, convert. To and *. parse, but the question is when C is used and when P is used.
The general explanation is as follows:
Convert is used to convert the types inherited from the object type (of course, all types are inherited from the object), but generally convert is used to convert the data directly to the obejct type, for example, when we use datareader to retrieve a data from the database, it is of the object type. If you use int. parse (Dr ["ID"]); will certainly report an error, so you must use convert. toint32 (Dr ["ID"]);
Parse is used to convert data of the string type.
As a matter of fact, we can see from the base class "system" that its convert class provides almost all conversions from type N to type N, that is, convert is only a conversion from the string type to the int type, only parse does not provide object conversion. It can be said that parse is of the N type (except for the string type, it does not provide parse conversion, but only supports convert conversion to other types ). All have one method, while convert provides a general conversion.
The question is, since convert provides general conversions such as string STR = "1234"; int32.parse (STR); Convert. toint32 (STR);, the same effect can be achieved. Which one should we use, and the effect will be better!
Let's look at the base class of system:
Take int-string type conversion without format conversion as an example. When int32.parse (STR) is used, it is actually calling the parse method in int32 of the construction type in the system class of the mscorlib base class library. This method calls an overload method (PARSE) in this constructor.

Public   Static   Int Parse ( String S)
{
ReturnParse (S, numberstyles. integer,Null);
}

 

 

In the overloaded parse, the read-only function nunber in the system is called.
Public   Static   Int Parse ( String S, numberstyles style, iformatprovider provider)
{< BR style =" margin: 0px; padding: 0px; "> numberformatinfo instance = numberformatinfo. getinstance (provider);
numberformatinfo. validateparsestyle (style);
return Number. parseint32 (S, style, instance);
}

 

 


Pasreint32 in nubmer performs the conversion.
Well, int analysis is here. View convert conversion,
When we call the convert. toint32 method, we can see how the convert class is executed!
Public   Static   Int Toint32 ( String Value, iformatprovider provider)
{
If (Value =   Null )
{
Return 0;
}
Return   Int . Parse (value, numberstyles. integer, numberformatinfo. getinstance (provider ));
}

 

 

  
You will find that he directly finds the parse method of the int class. This method directly executes the overload method of Parse in int32. It can be seen that they eventually need to fall into the number of read-only classes for type conversion. The performance gap is generated before the number is reached! In parse, he will find himself directly, and convert will convert string to what type will eventually fall on this type of parse, so the conversion of string type is still more efficient.

"

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.