I'm looking for a clear, concise and accurate answer.
Ideally as the actual answer, although links to good explanations welcome.
I'm looking for a clear, concise and accurate answer.
Ideally as the actual answer, although links to good explanations welcome.
from C# 3.0 In a Nutshell:
Like anything else, autoboxing can be problematic if not used carefully. The classic is to end up with a NullPointerException and not be able to track it down. Even with a debugger. Try this:
The .NET FCL generic collections:
were all designed to overcome the performance issues of boxing and unboxing in previous collection implementations.
For more, see chapter 16, CLR via C# (2nd Edition).
Boxing & unboxing is the process of converting a primitive value into an object oriented wrapper class (boxing), or converting a value from an object oriented wrapper class back to the primitive value (unboxing).
For example, in java, you may need to convert an
int
value into anInteger
(boxing) if you want to store it in aCollection
because primitives can't be stored in aCollection
, only objects. But when you want to get it back out of theCollection
you may want to get the value as anint
and not anInteger
so you would unbox it.Boxing and unboxing is not inherently bad, but it is a tradeoff. Depending on the language implementation, it can be slower and more memory intensive than just using primitives. However, it may also allow you to use higher level data structures and achieve greater flexibility in your code.
These days, it is most commonly discussed in the context of Java's (and other language's) "autoboxing/autounboxing" feature. Here is a java centric explanation of autoboxing.
Boxing and unboxing facilitates value types to be treated as objects. Boxing means converting a value to an instance of the object reference type. For example,
Int
is a class andint
is a data type. Convertingint
toInt
is an exemplification of boxing, whereas convertingInt
toint
is unboxing. The concept helps in garbage collection, Unboxing, on the other hand, converts object type to value type.In .Net:
Often you can't rely on what the type of variable a function will consume, so you need to use an object variable which extends from the lowest common denominator - in .Net this is
object
.However
object
is a class and stores its contents as a reference.While both these hold the same information the second list is larger and slower. Each value in the second list is actually a reference to an
object
that holds theint
.This is called boxed because the
int
is wrapped by theobject
. When its cast back theint
is unboxed - converted back to it's value.For value types (i.e. all
structs
) this is slow, and potentially uses a lot more space.For reference types (i.e. all
classes
) this is far less of a problem, as they are stored as a reference anyway.A further problem with a boxed value type is that it's not obvious that you're dealing with the box, rather than the value. When you compare two
structs
then you're comparing values, but when you compare twoclasses
then (by default) you're comparing the reference - i.e. are these the same instance?This can be confusing when dealing with boxed value types:
It's easy to work around:
However it is another thing to be careful of when dealing with boxed values.