Open In App

C# Boxing And Unboxing

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Boxing and unboxing are important concepts in C#. The C# Type System contains three data types that are Value Types (int, char, etc), Reference Types (object), and Pointer Types.

  • Boxing: It is a process that converts a Value Type variable into a Reference Type variable.
  • Unboxing: This process extracts the original value type from an object and converts it back to the value type.

It achieves the vice-versa. Boxing and Unboxing enable a unified view of the type system in which a value of any type can be treated as an object.

Example: Performing Boxing and Unboxing in C#.

C#
// Boxing and Unboxing in C#

using System;

public class Geeks 
{
	public static void Main(string[] args)
	{
		// Value type variable
		int n = 357; 

		// Boxing
		object box = n; 

		Console.WriteLine("Boxed value: " + box);

		// Unboxing
		int unbox = (int)box; 
		Console.WriteLine("Unboxed value: " + unbox);
	}
}

Output
Boxed value: 357
Unboxed value: 357

Explanation: In the above example we performed boxing and unboxing operations first we used a value type variable and and stored it in the referenced type variable which is an object here and then we again stored it in a value type unbox(int) variable and performed unboxing.

Boxing

The process of converting a Value Type variable (char, int etc.) to a Reference Type variable (object) is called Boxing. Boxing is an implicit conversion process where a value type is wrapped inside an object instance and stored on the heap. Value-type variables are generally stored on the stack when they are local variables. When they are fields within a reference type, they are part of the heap-allocated memory for the reference type. Reference type variables store memory addresses (references) on the stack, while their actual data is stored on the heap (except in some optimizations like String Interning).

int num = 23; // 23 will assigned to num

Object Obj = num; // Boxing

In the above first we declare a Value Type variable num of the type int and initialise it with value 23. Now, we create a Reference Type variable obj of the type Object and assign num to it. This assignment implicitly results in the Value Type variable num to be copied and stored in Reference Type variable obj as shown in below figure :

Boxing

Example: Demonstration of Boxing in C#.

C#
// C# program to demonstrate
// the Boxing
using System;
class Geeks
{
    static public void Main()
    {
        // assigned int value
        // 23 to num
        int num = 23;

        // boxing
        object obj = num;

		// Display the value of obj
		Console.WriteLine("Value of obj is: " + obj);

    }
}

Output
Value of obj is: 23


Unboxing

Unboxing is the process of explicitly converting a boxed object back into its original value type. It is an explicit conversion process. We can also say that the reverse process of boxing.

int num = 23; // value type is int and assigned value 23

Object Obj = num; // Boxing

int i = (int)Obj; // Unboxing

In unboxing first we create a Value Type integer i to unbox the value from obj. Unboxing requires explicit casting. The object must contain a valid boxed value of the correct type, or an InvalidCastException will be thrown. Thus, the Reference Type variable residing in the heap memory is copied to the stack as shown below figure:

Boxing

Example: Demonstrating the Unboxing in C#

C#
// C# implementation to demonstrate
// the Unboxing
using System;
class Geeks
 {

    static public void Main()
    {

        // assigned int value
        // 23 to num
        int num = 23;

        // boxing
        object box = num;

        // unboxing
        int unbox = (int)box;
    
     System.Console.WriteLine("Value of box: " + box);
	 System.Console.WriteLine("Value of unbox: " + unbox);
    }
}

Output
Value of box: 23
Value of unbox: 23

Note: If we try to perform unboxing of a null object or casting the object as an incompatible data type, the program throws exceptions like InvalidCastException.



Similar Reads