Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications.

We know that the boxing and unboxing are the spectacular concepts or important terminologies in the c# language. Basically, there are three types of the data types are held in the c# language. The boxing and unboxing are the terms that enabled us to the view in the system type in which the value can be treated as the any type of the object.

  • The Value Types are char, int, float, etc.
  • The Reference Types (The object types)
  • The pointer Types.

Firstly, let’s see Boxing in the c# language.

Boxing in c#:

  • Boxing is the data type that term defined as converting the value typed data type variable into the Reference type data type variable.
  • The process that converts the value typed to the reference typed objects is known as the Boxing the c# language.
  • The boxing is also considered as the implicit conversion that it is used in any object type.
  • The objects which are stored in the stack area is defined under the valued type data type and the values which are stored in the heap area is defined as the Reference type data type.

Let's see the example for Boxing type:

int val= 10; // the 10 value is assigned to the val
Object obj1 = val; // the Boxing type

Explanation: In the above example we assigned some value to the val as the Value Typed variable in the integer conversion. And we assigned 10 to the val. And we created obj1 as the reference typed variable to the Object. To the Object we assigned the val. In this process the val = 10 value will be copied to the Object Typed variable.

Let's see an example program for Boxing in c#

// C# program to implement the Boxing technique
// the Boxing in c#
using System;
class JTP
{


static public void Main()   // main method
{
		int val = 1000;  // assigning the int vale to the val


object obj1 = val; // Boxing technique


val = 500; // the changing of value
System.Console.WriteLine
("Value Typed -  The type value of number is : {0}", val);
System.Console.WriteLine
("Object Typed - The type value of obj1 is : {0}", obj1);
}
}

Output:

Boxing and Unboxing in c#

Unboxing in c#:

  • The unboxing in the c# language is vice versa of the Boxing. It is the process of converting the Reference Type data type variable to the Value Typed data type variable. This entire process is known as the Unboxing terminology in c#.
  • The unboxing is also considered as the explicit conversion that it is used in any object type process.

Let's see an example syntax for unboxing in c#:

int val = 10; // assign the 10 value to the valued typed variable
Object obj1 = val;  // Boxing technique
int i = (int)obj1;  // Unboxing technique

Explanation: In the above syntax we assigned some value to the valued typed variable (I.e.val). Here we created the Reference Typed variable that is obj1 which we used for Object type. We assigned the value 10 to the val Valued Typed variable. Again, we created the integer type variable to unbox the I value from the object typed. By using the Type casting process, we can evaluate the similar type.

Hence, we created the Reference Typed data type variable contributed in the Heap memory that can be copied into the stack memory.

Let's see a simple example program for the unboxing program in c#

// C# program to implement the Unboxing technique
// the Unboxing in c#
using System;
class JTP
{
	static public void Main()  // Main method
{


int val = 10;  // assigning the value 10 to the val
object obj1 = val; // Boxing technique
int i = (int)obj1;  // unboxing technique
Console.WriteLine("The Value of obj1 object is : " + obj1);  // display obj 1 value
Console.WriteLine("The Value of iis : " + i);  // display i value
}
}

Output:

Boxing and Unboxing in c#

Difference between Boxing and Unboxing in c#

      Boxing    Unboxing
Boxing is the data type that term defined as converting the value typed data type variable into the Reference typed data type variable.The unboxing is the process of converting the Reference Typed data type variable to the Value Typed data type variable
The boxing is also considered as the implicit conversion that it is used in any object typeThe unboxing is also considered as the explicit conversion that it is used in any object type process.