(original) C # learning note More about 05--variables 01--type conversion 01--implicit conversion

Source: Internet
Author: User

The fifth chapter of the variable more content

The contents of this chapter:

How to make implicit and explicit conversions between types
How to create and use enum types
How to create and use struct types
How to create and use arrays
How to handle string values

The first topic to be discussed is type conversion, that is, converting a value from one type to another.

Then we describe some other types of variables:

    enumeration --variable type, user defined a set of possible discrete values that can be used in a way that people can understand.

    structure --the type of the variable that is synthesized, consisting of a user-defined set of other variable types.

    Array --contains multiple variables of one type that can be indexed to access individual values.

These types are more complex than the simple types used earlier, but can make work easier to accomplish. Finally, learn about another string-related topic-basic string processing.

5.1 Type conversions

Type conversions take the following two forms:

    Implicit conversions : Conversions from Type A to type B can be done in all cases, and the rules for performing transformations are simple enough for the compiler to perform transformations.

    Explicit conversions : Conversions from Type A to type B can only be done in some cases, the rules of the conversion are complex, and some type of processing should be performed.

5.1.1-Implicit conversion

Implicit conversions do not require any work, and you do not need to write code in addition. Consider the following code:

If the type of var2 can be implicitly converted to the VAR1 type, the assignment statement involves an implicit conversion. It may also handle only two variables of the same type, and does not require an implicit conversion. For example, the values of ushort and char are interchangeable, because they can all store numbers between 0~65535, which can be implicitly converted between the two types, as shown in the following code:

ushortchar'a'= Sourcevar; Console.WriteLine ("sourcevar val: {0}", Sourcevar); Console.WriteLine ("destinationvar val: {0}"

The values stored here in the Sourcevar are placed in the Destinationvar. When you output a variable with two Console.WriteLine () commands, you get the following result:

97

Even though two variables store the same information, they are interpreted differently using different types.

Simple types have many implicit conversions; bool and string do not have implicit conversions, but numeric types have some implicit conversions. Table 5-1 lists the numeric conversions that the compiler can implicitly perform (remember that char is stored as a numeric value, so char is treated as a numeric type).

The implicit conversion rules for these types are: any type A, as long as its range of values is fully contained within the range of the value of type B, it can be implicitly converted to type B.

The reason for this is very simple . If you want to put a value in a variable that is outside the range of the variable's value, there is a problem. For example, a variable of type short can store a 0~32767 number, and a byte can store a maximum value of 255, so if you want to convert a short value to a byte value, there will be a problem. If short contains a value between 256~32767, the corresponding value cannot be placed in byte.

  however , if the value in the short type variable is less than 255, you should be able to convert the value, right? The answer is yes. Specifically, it is possible, but an explicit conversion must be used. Performing an explicit conversion is somewhat similar to "I already know you are warning about doing so, but I will be responsible for its consequences".

(original) C # learning note More about 05--variables 01--type conversion 01--implicit conversion

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.