---
title: "What is Boxing and Unboxing in C#?"  
description: "What is Boxing and Unboxing in C#?"  
author: "Revati S Misra"  
published: 2024-06-11  
updated: 2024-06-11  
canonical: https://www.mindstick.com/forum/160714/what-is-boxing-and-unboxing-in-c-sharp  
category: "c#"  
tags: ["c#", "boxing", "unboxing"]  
reading_time: 3 minutes  

---

# What is Boxing and Unboxing in C#?

What is [Boxing and Unboxing](https://www.mindstick.com/articles/167412/boxing-and-unboxing) in C#?

## Replies

### Reply by Ashutosh Patel

### C# Boxing and Unboxing

In C#, [boxing](https://www.mindstick.com/articles/33/value-type-and-boxing) and unboxing are two related concepts related to treating values ​​and expressions. Understanding these concepts is important for effective memory management and optimal performance in .NET applications.

#### Boxing

Boxing is the process of converting a value type (such as **int, float, struct, etc.**) to a reference type (specifically, an **object**). Create an object on this heap and copy the value to this other object.

## Syntax-

```cs
int num = 123;    // Value type
object obj = num; // Boxing
```

Here is, the integer `num` is boxed into an object `obj`.

#### Unboxing

Unboxing is the opposite of boxing. This involves converting the boxed product back to its corresponding value. This requires an explicit cast and of course copying the value back to a variable value type.

## Syntax-

```cs
object obj = 123;   // Boxing
int num = (int)obj; // Unboxing
```

Here is, The object `obj`unboxed into an integer `num`

## Example-

```cs
using System;
namespace myProgram{
	class Program
	{
		 public static void Main()
 		{
 			 // Boxing value
 			 int num = 42;
 			 object obj = num; // num is boxed into obj
  			Console.WriteLine("Boxed value: " + obj);
  			// Unboxing value
  			int unboxedNum = (int)obj; // obj is unboxed back to an int
  			Console.WriteLine("Unboxed value: " + unboxedNum);
 		}
	}
}
```

## Output-

```cs
Boxed value: 42
Unboxed value: 42
```

#### Key Points

## Boxing

- Conversion implicitly
- This includes creating objects on the heap.
- There may be performance overhead due to memory allocation and garbage collection.

## Unboxing

- Conversion explicitly
- It may throw an InvalidCastException if the object does not have a valid target type.

#### Performance Considerations

Boxing unboxing can have important business implications because,

- Boxing heaps allocates memory, which is costly in terms of performance compared to stack allocation.
- Frequent boxing and unboxing can increase garbage collection, affecting application performance.

#### Avoiding Boxing and Unboxing

Consider the following methods to avoid unnecessary boxing and unboxing,

- Use regular collections of objects (such as **List<T>**, **Dictionary<TKey, TValue>**) instead of irregular objects (such as **ArrayList**, **Hashtable**, **etc**.) to avoid value type boxing.
- Reduce the use of object type variables for storing values ​​unless absolutely necessary.

By carefully understanding and managing boxing and unboxing, you can write more efficient and better-performing C# code.

**Also, Read:** [How to sort object arrays by specific property in C#?](https://www.mindstick.com/forum/160713/how-to-sort-object-arrays-by-specific-property-in-c-sharp)


---

Original Source: https://www.mindstick.com/forum/160714/what-is-boxing-and-unboxing-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
