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:

long Add(long ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.