Wednesday, February 7, 2007

C# Boxing and UnBoxing

Certification Test Question:

Give an example that demostrates unboxing?


Answer:
Value type to object (boxing)
object back to value type (unboxing)

Example:
object o = 123;
int one2three = (int)o;

----------
object pi = 3.14159265
double dPI = (double)pi;

unboxing is object to ValueType

MSDN:
Unboxing extracts the value type from the object.

No comments: