C#:convert.toint32 () and Int. The difference between Parse () [Go]

Source: Internet
Author: User

The biggest difference between the two methods is their handling of the null value: Convert.ToInt32 (NULL) returns 0 without any exception, but int. Parse (NULL) produces an exception.

Do not understand Chu Convert.ToInt32 and int.parse () fine nuances, do not mess with, otherwise it may produce unpredictable results. For example, if you take a parameter page value from a URL, we know that the value is an int, so you can use the

Convert.ToInt32 (request.querystring["page"])

Or

Int. Parse (request.querystring["page"])

However, if the page parameter does not exist in the URL, then the former will return 0,0 may be a valid value, so you do not know that the URL is not at all there is no such parameter and continue the next processing, which may produce unexpected effects, In the latter way, there is no page this argument throws an exception, we can catch the exception and then do the corresponding processing, such as prompting the user to lack of parameters, rather than the parameter value as a limit processing.

Int. Parse is converting string to int
Convert.ToInt32 is the conversion of objects that inherit from object to int.
You get an object, you want to convert it to int, with Int. It is not possible to parse, but to use Convert.ToInt32.

Personal Summary:
(1) Convert.ToInt32 parameters are more, Int.parse can only convert string types.
(2) The parse is to convert a string into a int,char,double .... And so on, that is *. Parse (string) must be a string in parentheses.
(3) Convert can provide multiple types of conversions, that is, convert.* () can be of many types (including string) in parentheses.

C#:convert.toint32 () and Int. The difference between Parse () [Go]

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.