Implicit Versus Explicit Conversions

Simply stated, conversions are a way to treat an expression as being of a specified type. This gives rise to two different situations: Either the conversion is statically known by the compiler to be safe (no data loss can occur) or such guarantees do not exist.

In the former case, implicit conversions are permitted by the compiler. Primitive types form a good playground to illustrate this. For example, given an int variable, it’s possible to treat it as if it were a long because all Int32 values definitely fit in the range of the Int64 type. This particular conversion is a representation-changing conversion that needs runtime assistance because we’re stuffing a 4-byte value in an 8-byte one:

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.