---
title: "Explain The Concept Of Boxing And Unboxing ?"  
description: "Explain The Concept Of Boxing And Unboxing ?"  
author: "Anonymous User"  
published: 2019-12-12  
updated: 2023-05-30  
canonical: https://www.mindstick.com/forum/155589/explain-the-concept-of-boxing-and-unboxing  
category: ".net"  
tags: ["c#", "boxing", "unboxing"]  
reading_time: 3 minutes  

---

# Explain The Concept Of Boxing And Unboxing ?

[please explain](https://www.mindstick.com/forum/344/hi-rohith-i-am-new-for-mvc-please-explain-how-to-set-implicitly-isauthenticated-true) about the [Concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net) Of [Boxing And Unboxing](https://www.mindstick.com/articles/167412/boxing-and-unboxing) in .net

framework.

## Replies

### Reply by Aryan Kumar

Boxing and unboxing are two operations that allow you to convert between value types and reference types in C#.

**Boxing** is the process of converting a value type to a reference type. When you box a value type, the value is wrapped in an object instance. This object instance is then stored on the managed heap.

**Unboxing** is the process of converting a reference type to a value type. When you unbox a reference type, the value that is wrapped in the object instance is extracted and stored in a variable of the appropriate value type.

Boxing and unboxing are often used when you need to pass a value type to a method that expects a reference type, or when you need to return a value type from a method that returns a reference type.

For example, the following code boxes an integer value and passes it to a method that expects a reference type:

C#

```plaintext
int i = 10;
object o = i;
```

The following code unboxes a reference type and stores the value in a variable of the appropriate value type:

C#

```plaintext
object o = 10;
int i = (int)o;
```

Boxing and unboxing can be expensive operations, so you should avoid using them unnecessarily.

Here are some of the benefits of using boxing and unboxing:

- **Allows you to pass value types to methods that expect reference types.**
- **Allows you to return value types from methods that return reference types.**
- **Allows you to use value types with object-oriented features, such as inheritance and polymorphism.**

Here are some of the drawbacks of using boxing and unboxing:

- **Can be expensive operations.**
- **Can introduce a performance penalty.**
- **Can make your code less efficient.**

Overall, boxing and unboxing are powerful tools that can be used to improve the flexibility and interoperability of your code. However, you should use them sparingly and be aware of the potential performance implications.

### Reply by Nishi Tiwari

**[Boxing](https://www.mindstick.com/articles/33/value-type-and-boxing) and unboxing is an important concept in C#**. In C# there are three types of data type, value types (int, float), reference type (object, string) and pointer type. C# allows to convert vale type to reference type and vice versa. Boxing and unboxing enables to a unified view of type system in which any type of value can be treated as an object.

**Boxing can be defined as a process of converting vale data type to a reference data type**. It is used by object type and it is an implicit conversion. Value type and referenced type stored in stack and heap respectively.

For example

```
 int num=23 ;
Object obj=num;
```

![Explain The Concept Of Boxing And Unboxing ?](https://www.mindstick.com/mindstickforums/2384be7b-d0f4-4c5b-9721-639de28bab84/images/33f58f01-d28d-45a4-90b0-05b8433a5335.jpeg)\

**Unboxing can be defined as the process of converting reference type to value type**. It is a reverse process of boxing and it as an explicit conversion.

For example

```
int num=23;
Object obj=num;
 int i=(int)obj;
```

![Explain The Concept Of Boxing And Unboxing ?](https://www.mindstick.com/mindstickforums/2384be7b-d0f4-4c5b-9721-639de28bab84/images/fe3c669a-39f7-45f1-bea7-1bcba63f564a.jpeg)


---

Original Source: https://www.mindstick.com/forum/155589/explain-the-concept-of-boxing-and-unboxing

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
