13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13 Conversions13. ConversionsA conversion enables an expression of one type to be treated as another type. Conversions can be implicit orexplicit, and this determines whether an explicit cast is required. [Example: For instance, the conversionfrom 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 longint c = (int) b; // explicit conversion from long to intend example] Some conversions are defined by the language. Programs may also define their ownconversions (§13.4).13.1 Implicit conversionsThe 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 conversionsImplicit conversions can occur in a variety of situations, including function member invocations (§14.4.3),cast expressions (§14.6.6), and assignments (§14.13).The pre-defined implicit conversions always succeed and never cause exceptions to be thrown. [Note:Properly designed user-defined implicit conversions should exhibit these characteristics as well. end note]13.1.1 Identity conversionAn identity conversion converts from any type to the same type. This conversion exists only such that anentity that already has a required type can be said to be convertible to that type.13.1.2 Implicit numeric conversionsThe 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.113

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!