C# Implicit Conversions
DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT
The following are my notes from section 6.1 of the C# Language Specification
Heuristic Model
- Definitions . Add definitions for the chapter.
- Examples . After adding definitions, then add examples.
- Edit . After adding examples, then edit for readability etc.
My Personal Conventions
- ++++> means "converts to"
- section headers are underlined and bold
- terminology is italicized
- everything is presented in the order that it occurred in the section
Terminology in Order of First Occurrence
-
conversion
- lets us treat an expression as a specific type
- either convert a typed expression to another type, or
- give a type to an un-typed expression
- can be pre-defined or user-defined
- can occur in a variety of circumstances
- should always succeed and never throw exceptions
-
types
objectanddynamicare considered equal for the purposes of conversion
- requires an explicit cast
- occur only for expressions of type dynamic
- from any type to the same type
-
sbyte -
byte -
short -
ushort -
int -
uint -
long -
ulong -
char -
float - may cause loss of precision but never loss of magnitude
- never lose any information
-
no implicit conversions to the
chartype
- decimal-integer-literal 0 converts to any enum-type and to any nullable types whose underlying type is an enum-type
- the latter converts to underlying type and then wraps the result
- if there is an implicit conversion for a predefined non-nullable value type,
- then there is also an implicit conversion for its nullable equivalent
-
S++++>T? -
S?++++>T?
- null literal to any nullable type
- the result is the null value of the given type
-
any reference type ++++>
objectanddynamic -
class-type
S++++> class-typeTprovidedSderives fromT -
class-type
S++++> interface-typeTprovidedSimplementsT -
interface-type
S++++> interface-typeTprovidedSderives fromT -
array-type
Swith element typeSE++++> array-typeTwith element typeTEprovided-
SandTdiffer only in element type (e.g. same number of dimensions) -
Both
SEandTEare reference-types -
An implicit conversion exists from
SEtoTE
-
T
++++>
System.Array
and the interface it implements
S[]
++++>
IList<T>
and its base interfaces provided there is an implicit identity OR reference conversion from
S
to
T
System.Delegate
and the interfaces it implements
S
++++>
T
provided
-
Shas an implicit identity conversion toUand -
Uhas an identity conversion toT
S
to interface- or delegate-type
T
provided
-
Shas an implicit identity or reference conversion to an interface- or delegate-typeUand -
Uis variance-convertible toT
- require no checks at run-time because the compiler can prove they will always succeed
- never change the referential identity of the converted object, i.e.
- a reference conversion changes the type of the reference NOT the value nor type of the referred to object
-
comments:
- convert a value-type into a reference-type
-
process for non-nullable-value-types:
- allocate an object instance,
- then copy the value-type value into that instance
-
process for nullable-value-types:
-
if
HasValueis false, then create a null reference-type - otherwise, unwrap and convert the source value as normal
-
if
S
++++>
object
|
dynamic
|
System.Valuetype
| any interface-type implemented by
S
System.Enum
S
++++> reference-type
T
provided a boxing conversion exists for the non-nullable-value-type
T
++++> interface-type
I
provided
-
Thas a boxing conversion toJand -
Jhas an identity conversion toI
T
++++> interface-type
I
provided
-
Thas a boxing conversion to an interface- or delegate-typeJand -
Jis variance-convertible toI
System.ValueType
-
dynamic++++> any typeT -
it's dynamically bound: the conversion is sought at run-time to the run-time type
T - if no conversion is found, a run-time exception will be thrown
- it is the seeking of the conversion, not the conversion itself, that throws the exception
-
we can avoid dynamic binding by first converting
dynamictoobject
- the binding of the operation is suspended until run-time
-
int++++> -
sbyte,byte,short,ushort,uint,ulong - provided that the constant-expression is within the range of the destination type
-
long++++>ulongprovided that the value of the constant-expression is not negative
-
if
Tis a type paramenter, then -
T++++> -
its effective base class
C -
any base class of
C -
any interface that
Cimplements - ---
-
any interface
IinT's effective interface set -
any base interface of
I - ---
-
a type parameter
UprovidedTdepends onU - ---
-
a reference type of
Rprovided -
it has an implicit conversion to a reference type
Sand -
Shas an identity conversion toR -
an interface type
Iprovided -
it has an implicit conversion to an interface or delegate type
Jand -
Jis variance-convertible toI -
nullliteral ++++>TprovidedTis a reference type - comments:
-
if
Tis a known reference type, these are all implicit reference conversions - else, they are boxing conversions
- consists of three parts
- optional standard implicit conversion
- user-defined implicit conversion operator
- optional standard implicit conversion
- these do not have types in and of themselves
- may be implicitly converted to delegate types OR expression-tree types
- "Some conversions are defined by the language. Programs may also define their own conversions." (introduction)