• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between Boxing and Unboxing in C#

Boxing-and-UnboxingIn C#, all the value types are derived from the class object. So, a reference variable of type object can refer to any other value type. C# introduces two methods to Boxing and Unboxing, which links value type to reference type.

The basic difference between Boxing and Unboxing is that Boxing is the conversion of the value type to an object type whereas, on other hands, the term Unboxing refers to the conversion of the object type to the value type. Let us study the other differences between Boxing and Unboxing.

Content: Boxing Vs Unboxing in C#

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonBoxingUnboxing
BasicObject type refers to the value type.process of retrieving value from the boxed object.
StorageThe value stored on the stack is copied to the object stored on heap memory.The object's value stored on the heap memory is copied to the value type stored on stack.
ConversionImplicit conversion.Explicit conversion.
Exampleint n = 24;
object ob = n;
int m = (int) ob;

Definition of Boxing

Boxing is a procedure of converting a value type to an object type. Here, the value type is stored on the stack, and the object type is stored in the heap memory. This conversion of value type to the object type is an implicit conversion. You can directly assign a value to an object, and C# will handle the rest conversion. Let’s understand Boxing with an example.

Boxing

int i = 24;
object ob = i; // Box the integer type n into object type ob.

or
object ob1=21; // here also an object type ob1 refers to an integer type

In above code, the integer type i containing value 24 is stored on the stack and is copied to the object type ob. An object type is now referring to an integer value.  Now, the “int i” also contain value 24 and the “object type ob” also contain value 24, but both the values are independent of each other i.e. if you change the value of i, it won’t reflect the change in the value of ob.

Boxing consumes extra memory along with extra time. The reason is that a new object, which will refer to the value type, must allocate the memory space on the heap. Next, the value of the value type that is stored on the stack will be transferred to ob the object type, on the heap memory location.

Definition of Unboxing

The reverse of Boxing is Unboxing. Unboxing is a conversion of the object type to the value type. In Unboxing the value of boxed object type stored on the heap is transferred to the value type that is stored on the stack. Unlike Boxing, the Unboxing has to be done explicitly. The object type is explicitly cast to the value type, and the value type must be same as value the object type is referring to. Let’s understand the concept of Unboxing with an example.

Unboxing

int i = 24;
object ob = i; // Box the integer type n into object type ob.
int j = (int) ob; // Unbox the integer value stored in object type ob to integer type y.

The value stored in the object ob is retrieved by casting it to the type same as the object was referring to i.e. integer type “j”.

Unboxing also consumes more memory and more time. Since, when an object type has to be unboxed then the value of the object type stored on the heap has to be transferred to the new value type stored on the stack. The object type whose value that has been retrieved will be now available for garbage collection.

Key Differences Between Boxing and Unboxing

  1. In boxing, an object is made to refer as a value type. On the other hand, the process of retrieving the value back from the boxed object is called unboxing.
  2. A value type stored on a stack is copied to the object stored on heap memory. On the other hand, in unboxing, an object stored on heap memory is copied to a value type stored on stack memory.
  3. Boxing is an implicit conversion whereas, unboxing is an explicit conversion.

Conclusion

Both the boxing and unboxing consume more time and memory, and they are computationally expensive. They also lack in type safety and increases runtime overhead. It is always advised to avoid too much use of boxing and unboxing in the program.

Related Differences:

  1. Difference Between Stack and Heap
  2. Difference Between Type Casting and Type Conversion
  3. Difference Between Stack and Queue
  4. Difference Between int and long
  5. Difference Between new and malloc( )

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy