---
title: "Boxing and Unboxing"  
description: "Here I will Explain about Main Difference Between Boxing and Unboxing ."  
author: "Trusartta Kumar Mahanta"  
published: 2019-08-11  
updated: 2019-08-11  
canonical: https://www.mindstick.com/articles/167412/boxing-and-unboxing  
category: "c#"  
tags: ["c#", "asp.net", "boxing", "unboxing"]  
reading_time: 2 minutes  

---

# Boxing and Unboxing

#### What is the Difference Between Boxing and Unboxing?

[Boxing and Unboxing](https://www.mindstick.com/forum/155589/explain-the-concept-of-boxing-and-unboxing) is one of the most Important Concept in C#.

In Boxing and Unboxing Contain three Data Types

1-Value Type(int,char etc)

2-[Reference Type](https://www.mindstick.com/interview/235/what-is-difference-between-value-types-and-reference-types-in-vb-dot-net-or-c-sharp-value-types-vs-reference-types)(object)

3-Pointer Type()

**Boxing**

-Basically, value type to reference(object) type of Conversion is called Boxing.

-Boxing is [Implicit](https://www.mindstick.com/interview/827/what-are-implicit-objects-list-them) type Conversion Process.

-Here Value Stored in the Stack Memory and reference(object) Copied Stored on the [Heap Memory](https://www.mindstick.com/forum/161766/what-is-the-difference-between-stack-and-heap-memory-with-example).

Ex- int x=100;

Object obj=x; //---Boxing

Ex-using System;

public class Test{

[static public](https://www.mindstick.com/interview/2715/can-we-write-static-public-void-instead-of-public-static-void) void Main()

{

int val = 2020;

// Boxing

object o = val;

// Change the value of val

val = 9437;

Console.WriteLine("Value type of val is {0}", val);

Console.WriteLine("[Object type](https://www.mindstick.com/blog/398/difference-between-object-type-and-var-type) of val is {0}", o);

}

}

--------------------------------------------------------------------------------------------------------------

## Unboxing

Unboxing is Convert Reference type into value Type is Called Unboxing. This is the Wrong Concept.

Correct Ans-

If a value type which is converted into reference type is converted back to value type ,we call it as Unboxing, but while Performing unboxing Explicit Conversion is Required.

-Unboxing is [Explicit type Conversion](https://www.mindstick.com/forum/160325/explain-the-difference-between-implicit-and-explicit-type-conversion-in-javascript).

-In Unboxing the object stored on the Heap memory Copied stored on the Stack Memory.

Ex-

Value Type----------------Reference Type--------------Value Type

Int y=Convert.ToInt32(obj); ----------------Unboxing

Ex:

using System;

public class Test {

static public void Main()

{

int val = 2020;

// Boxing

object o = val;

// Unboxing

int x = (int)o;

Console.WriteLine("Value of o is {0}", o);

Console.WriteLine("Value of x is {0}", x);

}

}

Note: A Direct reference type can never be Converted to Value Type.

---

Original Source: https://www.mindstick.com/articles/167412/boxing-and-unboxing

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
