C # (int)/int. Parse ()/int. The difference between TryParse ()/convent.toint32 ()--Recommended use of Int.tryparse ()

Source: Internet
Author: User

One, (int) variable name [coercion type conversion]:

This conversion is primarily used for numeric type conversions, from int to long,float,double,decimal types, and implicit conversions are possible, but the use of a long type to an int type requires an explicit conversion, which is how the data type is converted, or a compilation error occurs.
The method is unconditional for floating-point numbers and loses precision.
Of course, this method can also be used to convert an object to int, but the value of object is given a value of type int, otherwise a compilation error is generated, and an object is null when an error occurs.
The last thing to avoid is to handle the conversion of char type to int type, otherwise the value returned is ASCII code, not the value you want.

two, Int. Parse (String type variable name)

The method is to convert a string of numeric content to an int type, or to throw a ArgumentNullException exception if the string content is empty or null, or to throw a FormatException exception if the string content is not a number The OverflowException exception is thrown if the string content represents a number that is outside the range that the int type can represent.
One of the things you can do with this approach is to handle only string content , and the string content can only be within the range represented by the int type.

Third, Int. TryParse (string s, out int result)

This is also the way to convert a string of numeric content to an int type, but that way is more than int. The good thing about parse is that it does not appear to be abnormal. If the conversion succeeds returns True, False is returned if the conversion fails. Obviously, the last parameter is the output value, if the conversion fails, the output value is 0, and if the conversion succeeds, the corresponding value is output.

Iv. Convert.ToInt32

This method not only converts a string to an int type, but also converts the value of another type to an int type. If the variable is an object or string type, when its value is null, it returns 0, without causing a program error, but if the value of this string type is string. Empty, which can still cause a program error when transitioning to int. This method rounds the floating-point number.
This method, like coercion, cannot be used to handle char types, otherwise the ASCII code is returned.

C # (int)/int. Parse ()/int. The difference between TryParse ()/convent.toint32 ()--Recommended use of Int.tryparse ()

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.