C # Basic Data type conversion _ Practical Tips

Source: Internet
Author: User

int x; Long y = 123456789101112; x = (int) y; Console.WriteLine (x);

Output results:

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

In the code above, the data is lost because the value of the long variable exceeds the maximum size that the int can hold, and any transformations such as this that might cause data loss or throw an exception require an explicit conversion (explicit);

Instead, an implicit conversion (implicit) is performed.

Check the code above with chencked

From the diagram that the above conversion throws out an overflow exception, the explicit conversion may not be safe, (strong twist of the melon is not sweet);

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

Let's take a look at the following example:

int i; Double d = 6.88; i = (int) d; Console.WriteLine (i);

Output Result: 6

The loss of precision like this we can call him narrowing conversion, C # and C or C + +, in C or C + + can be directly i=d. The C # compiler tells us that if you accept loss of precision, you have to use explicit type conversions.

Why do we initialize the float type with "F" after the number, because C # thinks a decimal constant like 2.3 is a double with a higher precision, and the compiler will lose precision

and refused to execute. So when you assign a value to float, you either explicitly convert the constant to a float, or you add "F" (lowercase) directly after the constant. Of course, we usually do it when we're dealing with floating-point numbers.

Double so you can avoid many types of conversions.

Now let's change the way:

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

Here's a quick list of some of the limitations of explicit conversions:

In a value type, you can only convert in numbers, characters (char), enumerations (enums);

BOOL cannot be converted directly to other types, and other types cannot be converted directly to bool type.

Part Two: Conversion of strings 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 a lot of overloaded methods in the parse method, and I'm not going to list them.

Convert is not extensible and supports only predefined quantity types; he allows conversions 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 thing in the 1.0 version of the time only double, from 2.0 only popular; TryParse () usage is similar to parse () method, except that he returns a bool value and assigns the value to the variable by out

The difference between him and him: TryParse conversion failure does not throw an exception, only returns FALSE, the value being converted is null, or the format is incorrect, overflow, and so on.

If the value type out is 0, if the character type is undefined, the Boolean 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, the ToString () method, any data type you can ToString, and ToString () can define their own conversion methods, ToString () is usually used the most, this is not much to say.

PS: Write a very messy, think of what to write, as "prose" see

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.