×

C# Type System

Boxing, Unboxing and Type Casting

Boxing and Unboxing are important concepts in C#. Boxing converts a value type into a reference type (object), whereas Unboxing converts the reference type back into its original value type. C# also supports implicit and explicit type casting for converting one data type into another.

Beginner 25 Minutes Monopoly IT Solutions Updated 2026-08-04
.NET 10 class training banner
5.0
5.0
4.6
Overview

What you'll cover

Learning objectives

  • Understand Boxing
  • Understand Unboxing
  • Understand Reference Type and Value Type
  • Understand Type Casting
  • Understand Implicit Casting
  • Understand Explicit Casting
  • Understand Performance Impact of Boxing

Prerequisites

  • Variables
  • Data Types
  • Value Types
  • Reference Types
  • Arrays

Related topics

  • Data Types
  • Reference Types
  • Value Types
  • Implicit Conversion
  • Explicit Conversion
  • Object Class
Theory

Concept breakdown

Boxing

Boxing is the process of converting a Value Type into a Reference Type (object). Boxing is performed automatically by the CLR.

Unboxing

Unboxing is the process of converting a Reference Type (object) back into its original Value Type. Unboxing requires explicit casting.

Type Casting

Type Casting is the process of converting one data type into another. C# supports both Implicit and Explicit type conversions.

Implicit Casting

Implicit (Widening) conversion is performed automatically when there is no possibility of data loss.

Explicit Casting

Explicit (Narrowing) conversion is performed manually using the cast operator when data loss may occur.

Syntax
Hands-on example

Boxing, Unboxing and Type Casting Example

Console Output
> "42"
> "42"
> "42"
Walkthrough

Step by step

  1. 1

    Create Value Type

    Create an integer variable named num.

  2. 2

    Perform Boxing

    Assign the integer to an object variable. The CLR automatically converts the value type into a reference type.

  3. 3

    Display Boxed Value

    Print the boxed object to the console.

  4. 4

    Perform Unboxing

    Convert the object back to an integer using explicit casting.

  5. 5

    Display Unboxed Value

    Print the unboxed integer.

  6. 6

    Box an Array

    Assign an integer array to an object variable.

  7. 7

    Understand Type Casting

    Study the difference between implicit and explicit type conversions.

Summary

Key takeaways

Key points

  • Boxing converts Value Type to Reference Type.
  • Unboxing converts Reference Type back to Value Type.
  • Boxing is an implicit conversion.
  • Unboxing requires explicit casting.
  • Boxing allocates memory on the managed heap.
  • Excessive boxing and unboxing reduce application performance.
  • Implicit casting is safe.
  • Explicit casting may cause data loss.

Advantages

  • Allows Value Types to be treated as Objects.
  • Supports Generic Collections in older .NET versions.
  • Enables polymorphic behavior.
  • Provides flexibility when working with object-based APIs.

Disadvantages

  • Consumes additional memory.
  • Creates extra objects on the managed heap.
  • Slower than direct value type operations.
  • Frequent boxing/unboxing impacts performance.
Real-world use

Collection Framework

Older collections like ArrayList store objects. When integers are added into an ArrayList, boxing occurs automatically. When retrieved, unboxing is required.

Interview prep

Frequently asked questions

Boxing is the process of converting a Value Type into a Reference Type (object).

Unboxing converts a Reference Type back into its original Value Type.

Boxing is an implicit conversion.

Because the CLR requires the programmer to specify the target value type.

It allocates memory on the heap and decreases performance.

Automatic conversion performed when there is no risk of data loss.

Manual conversion performed when there is a possibility of data loss.

Test yourself

Quick MCQ practice

1. Boxing converts ______.

2. Which conversion requires explicit casting?

3. Which keyword represents a Reference Type?

Apply it

Practice on your own

Coding exercise

Employee Boxing Example

Create integer, double and character variables. Box them into object variables and unbox them back into their original data types.

Assignment

  1. Create variables of different value types.
  2. Perform Boxing on each variable.
  3. Perform Unboxing.
  4. Demonstrate Implicit Casting.
  5. Demonstrate Explicit Casting.
  6. Print all outputs.

Tags

#csharp#boxing#unboxing#type-casting#implicit-casting#explicit-casting#value-type#reference-type

getintouch

Book for Live Demo!