BCA / B.Tech 14 min read

Boxing & Unboxing in ADO.NET

Boxing & Unboxing in ADO.NET in Hindi | Boxing and Unboxing in ADO.NET in Hindi:


  • Boxing and unboxing are important concepts in ADO.NET, which are related to the management of data types in the .NET framework's type system. Understanding these concepts helps in handling performance and data types when working with ADO.NET.
  • Boxing and unboxing is an important concept in .NET, especially when accessing data in ADO.NET. It gives developers the ability to handle different types of data, but its excessive use can affect the performance of the application.
  • By using proper management and optimization techniques, the need for boxing and unboxing can be reduced and the functionality and efficiency of an ADO.NET application can be increased.
Introduction of Boxing & Unboxing in ADO.NET in Hindi | Introduction to Boxing and Unboxing in ADO.NET:

In the .NET framework, there are two types of data types:

  • Value Types - such as int, float, double, etc. These are stored in stack memory.
  • Reference Types - such as object, string, class, etc. These are stored in heap memory.
  • ADO.NET is a data access technology used for data access and management in a .NET application. When working with ADO.NET, sometimes we need to convert value types to reference types, so that they can be used in the ADO.NET object model. This process is called boxing. Similarly, if we need to convert a reference type back to a value type, it is called unboxing.

Boxing in Hindi | Boxing:

Boxing is the process in which a value type is converted to a reference type (usually an object). When we send value types (such as int, float, etc.) to an object model of ADO.NET, it is automatically converted to an object type.

Example of boxing:


int number = 100;        // This is a value type
object obj = number;     // Boxing - converting a value type to an object type

In this, a value type (int) value named number has been boxed into an object. During this process, a new object is created in the heap and the value is stored in this object.

Unboxing in Hindi | Unboxing:

Unboxing is the reverse of boxing, in which an object type (reference type) is converted back to a value type. For unboxing, it is necessary that the object is actually the same type to which it has to be converted, otherwise it will give a runtime error.

Example of unboxing

object obj = 100;    // Boxing has happened
int number = (int)obj;   // Unboxing - converting an object type to an int type
In this, the obj object has been unboxed into an int. If there was any other data type in obj, then there would be an error in the unboxing process.

Use of Boxing & Unboxing in ADO.NET in Hindi | Use of Boxing and Unboxing in ADO.NET:

  • The use of boxing and unboxing in ADO.NET mainly occurs when we retrieve data from a database or insert it into a database.
  • After getting data from the database in ADO.NET, that data often returns in the form of an object.
  • Unboxing has to be used to convert it into value types.
  • For example, when we read data using SqlDataReader or DataTable, we have to do unboxing to convert that data into specific value types.
Examples of ADO.NET in Hindi | Example in ADO.NET:

SqlConnection connection = new SqlConnection("connection_string_here");
SqlCommand command = new SqlCommand("SELECT Age FROM Users WHERE UserId = 1", connection);

connection.Open();
SqlDataReader reader = command.ExecuteReader();

if (reader.Read())
{
    object ageObj = reader["Age"]; // Boxing - retrieved data in the form of an object
    int age = (int)ageObj;          // Unboxing - converting an object type to an int
    Console.WriteLine("Age: " + age);
}

connection.Close();

In this code:

SqlDataReader reads the data of the Age column from the database in the form of an object.
The ageObj object has been changed to a value type by unboxing it into an int.

ADO.NET in Object Model in Hindi | Object Model in ADO.NET:

The object model of ADO.NET provides various objects for accessing data:

  • Connection Objects: Establishes a connection with a data source (such as SqlConnection, OleDbConnection).
  • Command Objects: Used to execute SQL commands (such as SqlCommand, OleDbCommand).
  • DataReader Objects: Used to read data in forward-only and read-only mode.
  • DataSet and DataTable Objects: Used to collect and manage data.

Performance of Boxing & Unboxing in ADO.NET in Hindi | Performance of Boxing and Unboxing in ADO.NET: 

  • Excessive use of boxing and unboxing in ADO.NET can affect performance. During boxing and unboxing, extra memory is required and new objects are created in the heap, which can cause Garbage Collection (GC). This can slow down the application.
Example:

If there is a large number of value types in the database and they have to be boxed and unboxed repeatedly, then the performance can be reduced.
More boxing and unboxing also increases memory consumption, which increases the load on Garbage Collection.

Ways to avoid boxing and unboxing:

  • Nullable Types: Nullable Types can be used to avoid boxing, especially when the value can be null.
  • Generic Collections: Using Generic Collections (such as List<T>, Dictionary<T>) while processing data in ADO.NET is a good way to avoid boxing.
  • Avoid Frequent Casting: Avoid frequent type casting and unboxing.
  • Type-Safe Data Access: Strive for type-safe data access in ADO.NET.