C # data type conversion

Source: Internet
Author: User

1. Forced conversion;

Note that char cannot be forcibly converted to int. If forced conversion is used, the ASCII value of the original integer is obtained.This method is used to remove floating point numbers unconditionally without precision..

2. usage method: type. parse (string type variable );

It can be said that this is a very practical method,You can use the. tostring () method to convert any data type into a string, and then use "type. parse (string type variable)"To the desired type.To use this method, you can only process string content, and the string content can only be within the range that can be expressed by the target 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. Use the convert class;

For example, convert. toint32 (string or another type );This method is used to round 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.

C # data type conversion

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.