Brandon Driesen said:
public class Employee
{
}
public class Programmer: Employee
{
}
OKAY
=====
Employee e = new Programmer(); // boxing
Programmer p = (Programmer)e; // unboxing
Actually, boxing has nothign to do with this, since you are working with
classes. The first is an example of basic OO concepts, a Programmer is-a
Employee, thus can be treated as one. The second is a cast which tells the
compiler that you want e treated as a Programmer explicitly and are aware
that if it is not an exception will occur.
Boxing is the way the runtime treats value types(your standard int, char,
byte data types plus any struct or enum) as an object with virtual method
tables and the whole bit. Since value types do not have the object header
that is used to call virtual methods(such as interface methods and
ToString()), the sync block structure used by the lock keyword(and the
Montior class), among other things, you can't do something like "object o =
10" because the value 10 is just 4 bytes containing the bit pattern for the
number 10, it lacks all of the specifics of objects. Boxing gets around this
by copying the value and type information into a full reference type object
that can be treated like any other object. It is comparatively expensive
since it means copying your value and probably several times the size of
your value due to the added memory taken by the object header.
An actual example of boxing would be something like...
int unboxed = 10;
object boxed = unboxed; //boxing occurs here.
unboxed = (int)boxed; //unboxing occurs here.