×

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.  

Related Topics

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

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...

3 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.