Sunday, July 8, 2018

What is Boxing and Unboxing

    public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}
// Comment
public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}

// Comment
public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}

int? i = null; int j = i ?? 0; Console.WriteLine(j);
What is Boxing and Unboxing?

Answer:Boxing and Unboxing both are used for type conversion but have some difference:

Boxing:

Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLR boxes a value means when CLR is converting a value type to Object Type, it wraps the value inside a System.Object and stores it on the heap area in application domain.

Example:



Unboxing:

Unboxing is also a process which is used to extract the value type from the object or any implemented interface type. Boxing may be done implicitly, but unboxing have to be explicit by code.

Example:



The concept of boxing and unboxing underlines the C# unified view of the type system in which a value of any type can be treated as an object.
Share:

1 comment:

  1. DBMS assignments can be challenging when students have to work with SQL queries, normalization, database design, and ER diagrams. Understanding both the theory and practical implementation takes regular practice. Getting DBMS assignment writing support can help students approach these technical tasks more systematically. Clear guidance makes complicated database concepts easier to follow and can improve practical skills. It also helps learners organize their coursework, understand common errors, and develop greater confidence with database management systems.

    ReplyDelete

Developer Technology

Developer Technology

Upcoming Articles/Videos

Data Abstraction and Encapsulation Video
When to use Abstract class in C#
Copyright © .NET Developer | Powered by Shrimant Telgave Design by Shrimant Telgave