Implicit and display type conversions for C #

Source: Internet
Author: User

Today, when reading an article about data type, inadvertently saw two keywords, "implicit conversion" and "Display Transformation", and then suddenly think of the beginning to learn programming, also always in the code compile time encountered such problems.

Well, today is the time to summarize the relationship between the two.

Let's take a look at some of the following questions:

I first defined a variable:

string 5;

A person with a bit of common sense can see the problem at the same glance.

Then look at the following code:

Double 5 ; float f1 = d1;

As we expected, now we try to compile and then output F1 through the console.

The result altogether compile the time can not pass, reported the following exception:

And then look at the following code:

float 5 ; double D1 = F1; Console.WriteLine (F1);

And then compile and run. The result is a successful build and a perfect run:

In the face of the first compile-time conversion, we modify it slightly, as follows:

Double 5 ; float f1 = (float) d1;

Then compile, run, and the result is perfect to run:

It is strange to see such a scene.

This is related to the implicit conversion and display transformations we talked about earlier.


Implicit conversions: Conversions of data types are done automatically by the compiler, without the need for manual intervention of data conversions.

Display transformations: Conversions of data types that are contrary to implicit conversions, that is, data conversions that require human-forced intervention.

While the above three pieces of code compile and run, the first, the second section of the code conversion is implicit conversion, and the third code is to use the display conversion after the correct execution.

In what case, the conversion can be achieved with only an implicit conversion between the two data types, and under what circumstances it is necessary to cast.

Let's take a look at the following table:

After reading this table, we will find that the float type represents a 32-bit floating-point value, whereas a double type represents a 64-bit floating-point value. An implicit conversion is possible when a float is converted to a double, whereas a double conversion to float requires a cast, which is a display transformation.

The value range of the double type is greater than the value range of type float, so float to double can be implicitly convertible, double to float but need to display the conversion.

The following conclusions are drawn:

An implicit conversion can be performed when the value range of the converted type is less than the value range of the destination type, otherwise the implicit conversion is the compiler will report the exception. This means that large storage capacity data types can accommodate small storage capacity data types, and vice versa.

The conditions for implicit conversions are:

1. The value range of the converted type must contain the range of values for the target type;

2. The value of the converted type must be compatible with the target type.

And the conditions for the display transformation are:

1. The value of the converted type is to be in the value range of the target type, and if the maximum or minimum value of the target type is exceeded, the compiler throws an exception and the conversion is unsuccessful.

2. The values of the converted type must also be compatible with the target type.

Similarly, implicit conversions and display transformations also apply in reference types. Two classes are defined below.

    /// <summary>    ///Human/// </summary>     Public classPersion { Public intId {Get;Set; }  Public intSex {Get;Set; }  Public intAge {Get;Set; }  Public intHeight {Get;Set; } }      /// <summary>    ///Program Class/// </summary>     Public classprogrammer:persion { Public stringJob {Get;Set; }  Public stringpostion {Get;Set; } }

The fields of the above two classes are arbitrarily defined and may be unreasonable, just to illustrate the issue of their conversion.

Then do the following initialization and conversion:

            New 1  - 168 " Programmer " 1  };             = PM1;

After compiling, the display is compiled by:

Then look at another piece of code:

            New 1  - 1  the  };             = P2;

In the compilation, the result shows an exception:

Therefore, in a reference type, such as a conversion between a class and a class, it is also necessary to follow the principles and rules of implicit conversion and display transformations.

Of course, there will be data loss of conversion, which is unavoidable, it can only be said that developers in the use of these data type conversion to be able to understand what can be caused by the loss, and how to use them in the most reasonable way, in order to create unnecessary losses in the use process.

Implicit and display type conversions for C #

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.