---
title: "Explain boxing and unboxing with examples."  
description: "Explain boxing and unboxing with examples."  
author: "Anubhav Sharma"  
published: 2025-06-10  
updated: 2025-06-13  
canonical: https://www.mindstick.com/forum/161699/explain-boxing-and-unboxing-with-examples  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Explain boxing and unboxing with examples.

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) [boxing and unboxing](https://www.mindstick.com/articles/167412/boxing-and-unboxing) with examples in C# languages.**

## Replies

### Reply by ICSM Computer

### What is Boxing and Unboxing in C#?

> **[Boxing](https://www.mindstick.com/articles/33/value-type-and-boxing)** and **Unboxing** are concepts in C# related to **value types** (`int`, `bool`, `struct`, etc.) and **reference types** (`object`, `class`, etc.).

## Definitions

| Concept | Description |
| --- | --- |
| **Boxing** | Converting a **value type** to an **object** (or to any interface type it implements) |
| **Unboxing** | Extracting the **value type** from an object |

## Why Does It Happen?

C# treats value types and reference types differently:

- **Value types** are stored on the **stack**
- **Reference types** are stored on the **heap**
- To store a value type in a variable of type `object`, it must be **boxed** (copied to the heap).

## Example: Boxing

```plaintext
int number = 42;           // value type (stack)
object obj = number;       // BOXING (copy 'number' into heap)
```

### What happens:

- `number` is a value type.
- When assigned to `object obj`, it's **boxed**.
- A new **object** is created on the **heap** with a copy of `number`.

## Example: Unboxing

```plaintext
object obj = 42;           // boxing
int number = (int)obj;     // UNBOXING
```

- Unboxing extracts the value type from the object.
- You **must cast explicitly**.
- If the object doesn't contain the expected type, it throws `InvalidCastException`.

## Example with Error:

```cs
object obj = 42;
double num = (double)obj; // ❌ InvalidCastException
```

Even though both `int` and `double` are value types, the boxed `int` cannot be unboxed to a `double`.

## Performance Impact

- **Boxing and unboxing is relatively expensive**, especially in loops.
- Frequent boxing/unboxing can lead to **GC pressure** and **heap fragmentation**.

## Example with Collection

Before generics:

```cs
ArrayList list = new ArrayList();
list.Add(10);              // boxing
int x = (int)list[0];      // unboxing
```

With generics (avoids boxing):

```cs
List<int> list = new List<int>();
list.Add(10);              // no boxing
int x = list[0];           // no unboxing
```

## Summary

| Operation | Description | Explicit? | Heap/Stack |
| --- | --- | --- | --- |
| Boxing | Value → Object (value to reference) | No | Moves to heap |
| Unboxing | Object → Value (reference to value) | Yes | Copies to stack |


---

Original Source: https://www.mindstick.com/forum/161699/explain-boxing-and-unboxing-with-examples

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
