The difference between convert, Parse, and TryParse in C # (int)

Source: Internet
Author: User

Tag: Value log pre throws exception false logs cannot post number

Convert.ToInt32 and Int. The Parse is more similar, in fact Convert.ToInt32 internally called Int. Parse;

When the Convert.ToInt32 parameter is NULL, 0 is returned;

Int. Throws an exception when the Parse parameter is null;

An exception is thrown when the Convert.ToInt32 parameter is "";

Int. When the Parse parameter is "", an exception is thrown;

Convert.ToInt32 can be converted to more types;

Int. Parse can only convert strings of numeric types;

Int. TryParse and Int. Parse is more similar, but it does not produce an exception, the conversion succeeds and returns True, and the conversion fails to return false. The last parameter is the output value, and if the conversion fails, the output value is 0.

(int) is a cast conversion and can only convert other numeric types to int, which cannot convert a string, such as the following example:

String v = "1"; int n = (int) v;

Int. TryParse Example

int id = 1; Int. TryParse (request.querystring["id"], out ID);

If the ID value passed in by the QueryString is an integer, then the ID variable value is that number, and if the incoming is not an integer, then the ID variable value is 0 (not the initial value 1).

The difference between convert, Parse, and TryParse in C # (int)

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.