RobinS said:
It takes a lot of resources, so it should be minimized.
Not really. It does consume resources and is not free, so should be
minimized, but it certainly doesn't consume a "lot" of resources.
Boxing creates a reference type, on the heap, that contains the boxed
value and a type code. So, there's the modest cost of allocation
(which does push the system closer to the next garbage collection) and
the modest cost of copying data.
Unboxing copies the value type from the boxed value to a new copy of
the value type. So, there's the modest cost of copying the value type.
I did some tests a while back. Populating an ArrayList with integers,
then reading them all back, takes about twice as long as populating a
List<int> with integers and then reading them all back. A large ratio,
and one that can make a difference in a bottleneck routine, but not a
large absolute difference - neither operation is particularly slow.