Data type conversions in C #

Source: Internet
Author: User

Data types can be converted to each other under certain conditions, such as converting int data into double type data. C # allows the use of two transformations: implicit and explicit conversions.

1. Implicit conversion

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.

Implicit conversions do not require any work, and you do not need to write code in addition. If you convert int data to double type data:

int Ten ; double B = A; // Implicit conversions

The implicit conversion rule is that any type a can be implicitly converted to type B as long as its range of values is fully contained within the range of the value of type B. Based on this translation rule, implicit conversion of C # does not result in data loss. It is important to note that there is no implicit conversion of the simple type bool and string that we use most often.

2. Explicit conversion

Explicit conversions: Conversions from Type A to type B can only be done in some cases, the conversion rules are complex, and some kind of extra processing should be performed. Explicit conversions are also called coercion type conversions, and explicit conversions require the user to explicitly specify the type of conversion. If you convert a double type data to an int type data:

            Double 10.5 ;             int d = (int) C; // Show Transformations

Remind:

(1), an explicit conversion may result in an error. When this conversion is made, the compiler will overflow detect the transformation. If there is an overflow stating that the conversion failed, it indicates that the source type is not a valid target type. Type conversions are not possible.

(2), coercion type conversion will result in data loss, as in the above example, the resulting D value is 10.

3. Type conversion by method

(1), use the ToString () method. All types inherit the object base class, so there is the ToString () method (which converts to a string).

(2), through Int. Parse () method conversion, parameter type only supports string type. Note: The value of string cannot be null when using this method, otherwise it cannot be converted, and the string type parameter can only be a variety of integers, cannot be floating-point, or it cannot be converted (for example, Int. Parse ("2.0") cannot be converted).

            int i;             int. Parse ("+");

(3), through Int. The TryParse () method converts the conversion method to int. The Parse () transformation method is similar to the difference between int. The Parse () method cannot be converted successfully, and the method performs normally and returns 0. meaning int. The TryParse () method is more than int. The Parse () method has more than one exception handling, returns False if an exception occurs, and returns the output parameter to 0.

            int i;             string NULL ;             int. TryParse (S, out i);

(4), conversion through the Convert class, the Convert class provides a lot of conversion methods. The premise of using these methods is that you can convert the objects you want to convert to the appropriate type, and if you cannot convert, you will report the wrong format. Note: When using Convert.ToInt32 (double value), if value is a number in the middle of two integers, the even numbers are returned, that is, 4.5 is converted to 4, and 5.5 is converted to 6.

(5), to achieve their own conversion, through the inheritance interface iconventible or Typeconventer class, so as to achieve their own conversion.

4. Convert using as operator

Use the as operator to convert, but as can only be used for reference types and nullable types. There are many advantages to using as, and when a type conversion is not possible, the object is assigned a value of NULL, avoiding the type conversion times error or exception. C # Throwing exceptions is very resource-intensive for catching exceptions and processing, and if you just assign the object to null, it is almost non-expendable (consumes very little resources).

5. Packing and Unpacking

Boxing and unpacking a bridge between the value type and the reference type, so that any Value-type value can be converted to a value of type object, which in turn can be converted.

Boxing: Boxing refers to data that implicitly converts data of a value type to an object type (object). When you perform a boxing operation, it is unavoidable to request memory space on the heap and copy the value type data on the stack to the requested heap memory space, which is sure to consume memory and CPU resources. Note: You can also use an explicit conversion when you perform a boxing conversion.

Unpacking: Unpacking refers to explicitly converting data of an object type to a value-type data. The unboxing process is a boxing inverse process, which converts the value of the reference type stored on the heap to a value type and assigns a value type variable. The unpacking operation is divided into two steps: one is to check the object instance, to ensure that it is a boxed value of the given value type, and to copy the value from the instance to the value type variable.

Packing and unpacking are used to consume memory and CPU resources, and also to reduce efficiency, so try to avoid them.

Data type conversions in C #

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.