C # type conversion,

Source: Internet
Author: User

C # type conversion,

I. Overview of common types:

Bool-> System. Boolean (Boolean type, its value is true or false)
Char-> System. Char (Bytes type, occupies two bytes, indicating 1 Unicode character)
Byte-> System. Byte (byte, which occupies 1 Byte, indicates an eight-digit positive integer, ranging from 0 ~ 255)
Sbyte-> System. SByte (in byte notation, which occupies 1 byte, indicates an 8-digit integer, range:-128 ~ 127)
Ushort-> System. UInt16 (unsigned short integer, which occupies 2 bytes and represents a 16-bit positive integer in the range of 0 ~ 65,535)
Uint-> System. UInt32 (unsigned integer, 4 bytes, representing a 32-bit positive integer, range: 0 ~ 4,294,967,295)
Ulong-> System. UInt64 (unsigned long integer, which occupies 8 bytes, indicates a 64-bit positive integer in the range of 0 ~ About 10 to the power of 20)
Short-> System. Int16 (short integer, which occupies 2 bytes, indicates a 16-bit integer, range:-32,768 ~ 32,767)
Int-> System. Int32 (integer, 4 bytes, representing a 32-bit integer, ranging from-2,147,483,648 to 2,147,483,647)
Long-> System. Int64 (an integer of 8 bytes, representing a 64-bit integer. The value range is about-(19 of 10) to the 19th power of 10)
Float-> System. Single (Single-precision floating point, 4 bytes)
Double-> System. Double (double-precision floating point type, 8 bytes)

For more information about the value type and reference type, see: Http://www.cnblogs.com/Liyuting/p/6699536.html

Ii. explicit and implicit conversions:

Implicit conversion:You do not need to specify the conversion type in the Code for implicit conversion. For example:

Int intNumber = 10; double doubleNumber = intNumber; long a = intNumber; // hide

Explicit conversion:Explicit conversions are the opposite. You must specify the conversion type. For example:

Double doubleNumber1 = 10.1; int intNumber1 = (int) doubleNumber1; // display

For the basic data type that represents a value, the data type with a small value range can be converted to a data type with a large value range by implicit conversion. In turn, the display type must be converted. Just like the two examples above. For the class type, the subclass can be converted to the parent class for implicit conversion, but in turn must be converted explicitly, for example, string str1 = "abc"; object obj = str1; // convert the subclass to the parent class and implicitly convert string str2 = (string) obj; // convert the parent class to the subclass. If there is no inheritance relationship between the two classes, you cannot perform implicit or explicit conversions. In this case, you must define an implicit or explicit conversion method in the converted class.

Explicit conversions must be of the same type, that is, the two data types must be compatible. Implicit conversions are upward conversions (equivalent to subclass-to-parent conversions ), the forced type conversion is a downward Transformation (equivalent to the parent rotor class), just as the Double type can contain the int type. The forced conversion can be of the same type (like the class at the same level as class1 and class2), both of which are parsed on the content. Convert. toInt32 and int. all Parse are forced conversions, int. parse converts String to int (in many cases, some optimizations may be made, or just for convenience, the processing logic is the same), while Convert. toInt32 is the int type of the Object inherited from the Object (18 types of overloading ). for example, if you want to convert an object to an int, use int. parse is not allowed. Convert is used. toInt32.

Iii. packing and unpacking:

Packing: Convert the value type to the reference type.

Binning: converts a reference type to a value type.

Iv. IS and:

1. Use Cases:

Forced type conversion may cause exceptions. Is and as are designed to solve this problem. is and as will never throw an exception.

2. is checks whether an object is compatible with the specified type, and considers the Rys replacement. Dog is Animal, while Animal is not Dog.

3. as is the same as forced type conversion. The difference is that it is safe to use. If the conversion fails using as, Null is returned and no exception is thrown.

4. Use is and as to replace forced type conversion, as shown below:

A. IS only used to judge the type compatibility and does not perform real type conversion. Returns true or false. No null is returned. If the object is null, false is returned. The is operator will never throw an exception. If (a is Dog) {Dog d = (Dog) ;...
Boolean b1 = (obj is Object); // b1 is true} B. The AS operator is similar to forced conversion, but different. When the Object is null, instead of throwing an exception, null is returned. Dog d = a as Dog; if (d! = Null ){...
Class1 B = o as Class1; // B = null
}

Note: The effects of the two are the same, but the efficiency difference is great. Using is will check the type of the object twice, one is verification, and the other is forced conversion. Only one object type check is performed using. Check whether the object type is a resource-consuming operation. First, you must determine the actual type of the object. Then, you must traverse the inherited tree structure (hierarchy) to check with each base class.

 

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.