C # standard interpretation 8.2.2 type conversion

Source: Internet
Author: User

The predefined type also contains the predefined conversion behavior. For example, there is a predefined conversion between int and long. C # differentiate between the two types of conversions: implicit conversion and display conversion. Implicit conversions refer to safe conversions that do not need to be pointed out carefully. For example, int to long conversion is implicit conversion. This conversion is always successful and will not cause information loss ). The following example: 1 using System;
2 class Test
3 {
4 static void Main ()
5 {
6 int intValue = 123;
7 long longValue = intValue;
8 Console. WriteLine ("{0}, {1}", intValue, longValue );
9}
10} implicitly converts an int type variable to a long type variable ). On the contrary, a conversion expression is required to display the conversion, for example: 1 using System;
2 class Test
3 {
4 static void Main ()
5 {
6 long longValue = Int64.MaxValue;
7 int intValue = (int) longValue;
8 Console. WriteLine ("(int) {0 }={ 1}", longValue, intValue );
9}
10} In the example, the long display is converted to int. The output is (int) 9223372036854775807 =-1 because of overflow. Conversion expressions allow display and 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.