This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!
11.5 Narrowing conversions
Narrowing conversions are conversions that cannot be proved to always succeed, conversions that are known to possibly lose information, and conversions across domains of types sufficiently different to merit narrowing notation. The following conversions are classified as narrowing conversions:
- From any type to a more derived type.
- Numeric types narrow in the following progression, from left to right:
Double
, Single
, Decimal
, Long
, Integer
, Short
, Byte
. See the section below on how narrowing numeric conversions are performed.
- From
Boolean
to any numeric type. The literal True
converts to the literal 1
. The literal False
converts to the literal 0
.
- From any numeric type to
Boolean
. A zero value converts to the literal False
. Any other value converts to the literal True
.
- From any numeric type to any enumerated type.
- From any enumerated type to any type its underlying type has a narrowing conversion to.
- From any enumerated type to any other enumerated type. A narrowing enumeration conversion between two types is processed by treating any participating enumerated type as the underlying type of that enumerated type, and then performing an widening or narrowing numeric conversion between the resulting types.
- From any class type to any interface type, provided the class type is not final and does not implement the interface type.
- From any interface type to any type, provided the type is not final or the type implements the interface type. Note that converting an interface to a value type consists of copying the value in the interface instance to the target variable (which may be a temporary).
- From any interface type to any other interface type, provided there is no inheritance relationship between the two types.
- From an array type
S
with an element type SE
to a covariant array type T
with an element type TE
, provided all of the following are true:
S
and T
differ only in element type
- Both
SE
and TE
are reference types
- An narrowing reference conversion exists from
SE
to TE
- From
String
to Char
- From
String
to Char()
and Char()
to String
- From any interface type to any value type that implements the interface type.