
Understanding Boxing Versus Unboxing in .NET In your
day to day coding, are you converting primitives to objects and objects
to primitives? If YES, then do you know the consequences of it? This article
will help you in understanding the logic involved in this conversion and
drawbacks of it. Before you get into Boxing and Unboxing, you should be
aware of the terms Value Type and Reference Type.
All Primitive
Types are Value Types. Value Type Objects will be associated with a default
value and when a value is assigned to it, the concerned variable will
be directly holding that value. When value is assigned from one value
type variable to another, the value gets copied and both the value type
Objects are independent of each other. Value Types can be in boxed form
or unboxed form. Reference
Type variables will hold memory address of object stored in managed heap.
The word reference means that the concerned variable will hold only address
of the Object and not the Object in itself. More than one reference type
variable can point to the same Object. In that case, they will be accessing
the same Object and not its copy. Hence if content of the Object is modified,
then both the reference type variables will be pointing to the modified
content. Reference Types can only be in boxed form. With this
brief introduction on Value Types and Reference Types, you are now ready
to know about Boxing and Unboxing. Conversion of Value Type Object into
Reference Type Object is known as Boxing and the vice versa (Conversion
of Reference Type Object into Value Type Object) is known as Unboxing.
See the code sample below for further understanding of these concepts. class TestBoxing{ Hope you
are now clear with the difference between Value Type, Reference Type and
Boxing, Unboxing. Value Types are unmanaged and they dont require
a space in managed heap. When you are performing Boxing, the content is
copied to managed heap and in addition some overhead for pointers is also
added to memory size occupied. Hence Boxing requires considerable memory
usage. On the other side when Unboxing is performed, memory occupied by
the corresponding reference type is released. However certain additional
checks are performed before conversion. Though memory is released, it
has to wait for garbage collection to happen. Only then the memory can
be reused. Hence it is very obvious that both Boxing and Unboxing are
performance concerns. You should avoid unnecessary usage of Boxing and
Unboxing in your code. Here is an example where Boxing and Unboxing is
performed, though it can be easily avoided. Console.WriteLine
(valTypeObj1 + , + refTypeObj1); This is a
commonly used statement in most of the programs. But the concatenation
operator + requires Object at both ends. Hence valTypeObj1
is boxed internally and concatenated with refTypeObj1. You can easily
avoid this issue by using two different Console.WriteLine statements one
for each variables. Also try
to use strong typed arrays instead of collections to avoid Boxing and
Unboxing overhead. This article
will add value to your work if you can spare sometime to revisit your
code and eliminate unnecessary Boxing and Unboxing activities. Thereby
you can save memory for your application and increase performance as well.
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |