logo
CSharp_Prog_Guide

Результат

The value-type value = 456

The object-type value = 123

Unboxing Conversion

Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. An unboxing operation consists of:

The following statements demonstrate both boxing and unboxing operations:

int i = 123; // a value type

object o = i; // boxing

int j = (int)o; // unboxing

The following figure demonstrates the result of the previous statements.

Unboxing Conversion

For the unboxing of value types to succeed at run time, the item being unboxed must be a reference to an object that was previously created by boxing an instance of that value type. Attempting to unbox null or a reference to an incompatible value type will cause an InvalidCastException.