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{
static void main( ){
Int32 valTypeObj1, valTypeObj2; //Value Types, by default 0 is assigned to it
Object refTypeObj1, refTypeObj2; //Reference Types, null is assigned to it
valTypeObj1 = 101;
valTypeObj2 = valTypeObj1; // valTypeObj1 content is copied to valTypeObj2
valTypeObj2 = 105; // valTypeObj1 is still 101 and not 105
refTypeObj1 = valTypeObj1; //Boxing is performed
refTypeObj2 = refTypeObj1; //Both are now holding address of content 101
valTypeObj2 = (Int32) refTypeObj1; //Unboxing is performed
}
}

Hope you are now clear with the difference between Value Type, Reference Type and Boxing, Unboxing. Value Types are unmanaged and they don’t 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.

| Additional Ways of Ensuring Security in .NET | Memory Lifecycle in .NET – An Overview | Few Best Coding Practices for ASP .NET | Handling Session Efficiently Using SQLSERVER State Management in .NET | How to Restrict a Program to Single Instance in .NET? | How to Use Structured Exception Handling in .NET | Understanding Boxing Versus Unboxing in .NET | Understanding Different Levels of Security in .NET | Understanding the Disadvantages of Memory Management in .NET | Using Membership API for Secured Coding in .NET |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.