X hits on this document

758 views

0 shares

0 downloads

0 comments

110 / 396

Chapter ‎18   Unsafe code

6. Conversions

A conversion enables an expression of one type to be treated as another type. Conversions can be implicit or explicit, and this determines whether an explicit cast is required. For instance, the conversion from type int to type long is implicit, so expressions of type int can implicitly be treated as type long. The opposite conversion, from type long to type int, is explicit and so an explicit cast is required.

int a = 123; long b = a;// implicit conversion from int to long int c = (int) b;// explicit conversion from long to int

Some conversions are defined by the language. Programs may also define their own conversions (§‎6.4).

6.1 Implicit conversions

The following conversions are classified as implicit conversions:

Identity conversions

Implicit numeric conversions

Implicit enumeration conversions.

Implicit reference conversions

Boxing conversions

Implicit constant expression conversions

User-defined implicit conversions

Implicit conversions can occur in a variety of situations, including function member invocations (§‎7.4.3), cast expressions (§‎7.6.6), and assignments (§‎7.13).

The pre-defined implicit conversions always succeed and never cause exceptions to be thrown. Properly designed user-defined implicit conversions should exhibit these characteristics as well.

6.1.1 Identity conversion

An identity conversion converts from any type to the same type. This conversion exists only such that an entity that already has a required type can be said to be convertible to that type.

6.1.2 Implicit numeric conversions

The implicit numeric conversions are:

From sbyte to short, int, long, float, double, or decimal.

From byte to short, ushort, int, uint, long, ulong, float, double, or decimal.

From short to int, long, float, double, or decimal.

From ushort to int, uint, long, ulong, float, double, or decimal.

From int to long, float, double, or decimal.

From uint to long, ulong, float, double, or decimal.

From long to float, double, or decimal.

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.97

Document info
Document views758
Page views758
Page last viewedFri Dec 02 23:05:46 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments