×

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# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute read.

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# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

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# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

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# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

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# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

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

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# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

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

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.