C # basic data type conversion

Source: Internet
Author: User

 

Int x; long y = 123456789101112; x = (int) y; Console. WriteLine (x );

Output result:

We know that the value range of the long type is-9223372036854775805 ~ + 9223372036854775807; int type:-2147483648 ~ + 2147483647

In the code above, the value of the long variable exceeds the maximum value that the int can hold, resulting in data loss; explicit conversions (explicit) are required for any conversions that may cause data loss or exceptions );

The opposite is to execute implicit conversions (implicit ).

Use chencked to check the above Code

The figure shows that the above conversion throws an overflow exception, and the explicit conversion may not be safe (the strong twist is not sweet); but if

The value of long is within the valid value range of int, so that no exception is thrown.

Here is an example:

Int I; double d = 6.88; I = (int) d; Console. WriteLine (I );

 

Output result: 6

If the precision is lost in this way, we can call it narrow conversion. C # is different from C or C ++, in C or C ++, you can directly convert I = d. the C # compiler tells us that if you accept the loss of precision, you must use Explicit conversions.

Why should we add "F" after the number when initializing the float type? Because C # thinks that constants with decimal points like 2.3 have a double type with higher precision, the compiler will lose precision

And the execution is rejected. Therefore, when assigning a value to a float, you can either explicitly convert a constant to a float value, or directly add "F" after a constant (either in lower case ). of course, we generally prefer to use it directly when processing floating point numbers.

In this way, many types of conversions can be avoided.

Now let's change the method:

If we assign a low-precision value to a high-precision variable like this, we call it a wide conversion.

Here are some restrictions on Explicit conversions:

Values can only be converted to numbers, characters (char), and enumeration (enum;

You cannot directly convert bool to another type. Other types cannot be directly converted to bool.

Part 2: Conversion of string and value types

Mainly used: Parse (), Convert, ToString ()

String = "value type:

String aa = "123"; int num = int. Parse (aa); double db = double. Parse (aa );

Of course, there are still many overload methods in the parse method, so I will not list them one by one.

Convert is not extensible and only supports predefined types. It allows conversion from any basic type to other basic types.

String str = "true"; bool B = Convert. ToBoolean (str )? Bool. Parse (str): false; Console. WriteLine (B );

Output result: True

TryParse heard that this item was available only for double in Version 1.0, which was popularized from version 2.0. TryParse () is also used in the same way as the Parse () method, but it only returns the bool value, assign a value to a variable through out

The difference between the two methods: TryParse conversion failure does not cause an exception, but only returns false; the converted value is null, or the format is incorrect, overflow, and so on,

If the value is out of the numeric type, it is 0, if it is a character type, it is undefined, and the Boolean value is false ,....

String str1 = "abc", str2 = "123"; int a, B; int. tryParse (str1, out a); int. tryParse (str2, out B); Console. writeLine (a); Console. writeLine (B );

Output result: 0

123

Value Type = "string:

Finally, it is the tostring () method. You can use toString for any data type, and toString () can define the conversion method by yourself. tostring () is the most commonly used method, this is not much to be said.

Ps: the writing is messy. Let's take a look at what we think to write.

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.