Difference between forced conversion, Convert conversion, and Parse conversion in C #

Source: Internet
Author: User

In the past, I was often confused about the conversion methods of these data types. In order to thoroughly understand the differences and advantages and disadvantages between them, I checked some information on the Internet and personally verified it. I wrote it here, on the one hand, I would like to give some guidance to my friends who have similar experiences. On the other hand, I hope the elders can guide me and see where there is something inappropriate.

1 (int) variable name [Forced type conversion]:

This conversion method is mainly used for numeric type conversion, from int type to long, float, double, decimal type, you can use implicit conversion, however, Explicit conversions from the long type to the int type are required, that is, the data type conversion method. Otherwise, a compilation error occurs.

This method is used to remove floating point numbers unconditionally without precision.

Of course, this method can also be used to convert an object to an int type. However, the object value must be assigned a value of the int type. Otherwise, a compilation error occurs and an error occurs when the object is null.

Do not use it to convert the char type to the int type. Otherwise, the returned value is ASCII code rather than the expected value.

2 int. Parse (string type variable name)

This method converts the numeric content string to the int type. If the string content is null or null, an ArgumentNullException exception is thrown. If the string content is not a number, a FormatException exception is thrown; if the number indicated by the string content exceeds the range indicated by the int type, an OverflowException exception is thrown.

This method can only process string content, and the string content can only be within the range that can be expressed by the int type.

3 int. TryParse (string s, out int result)

This method also converts the numeric content string to the int type, but this method is superior to int. Parse, that is, it will not encounter exceptions. If the conversion is successful, true is returned. If the conversion fails, false is returned. Obviously, the last parameter is the output value. If the conversion fails, the output value is 0. If the conversion is successful, the corresponding value is output.

4 Convert. ToInt32

This method not only converts a string to the int type, but also converts other types of values to the int type. If the variable is of the object or string type and its value is null, 0 is returned, which does not cause program errors. However, if the value of this string type is of the string type. empty will still cause program errors when it is converted to int.

This method rounds the floating point number.

This method is the same as forced conversion. It cannot be used to process the char type. Otherwise, ASCII code is returned.

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.