There are lot of resources available on internet for Boxing and UnBoxing. I have just tried to make it as simple as possible for the reader to understand the difference between Boxing and Unboxing;
Here is a simple approach to understand Boxing and UnBoxing.
Boxing
Boxing is nothing but the process of conversion of Value types to Type Object, any interface type which is implemented by the value type. The C# compiler allow the conversion of value type to reference type and vice versa to value type.
while Boxing, value of a value type is allocated to the instance of an object and the value is copied over to the instance
Mainly the operation of converting a particular value type to a reference type is called BOXing.
Example of Boxing
class BoxingTest
{
static void Main() {
int IVariable = 1;
object Boxing = IVariable; // boxing
}
}
Unboxing:
If any value value type is extracted from the object is unboxing, Precisely Converting a value of a reference type to a value type is called unboxing.
Example of Unboxing
class UnBoxingTest
{
static void Main() {
int IVariable = 1;
object BoxingObj = IVariable; // boxing
int valuetyp = (int)BoxingObj; // unboxing
}
}
Here is a simple approach to understand Boxing and UnBoxing.
Boxing
Boxing is nothing but the process of conversion of Value types to Type Object, any interface type which is implemented by the value type. The C# compiler allow the conversion of value type to reference type and vice versa to value type.
while Boxing, value of a value type is allocated to the instance of an object and the value is copied over to the instance
Mainly the operation of converting a particular value type to a reference type is called BOXing.
Example of Boxing
class BoxingTest
{
static void Main() {
int IVariable = 1;
object Boxing = IVariable; // boxing
}
}
Unboxing:
If any value value type is extracted from the object is unboxing, Precisely Converting a value of a reference type to a value type is called unboxing.
Example of Unboxing
class UnBoxingTest
{
static void Main() {
int IVariable = 1;
object BoxingObj = IVariable; // boxing
int valuetyp = (int)BoxingObj; // unboxing
}
}
Comments
Post a Comment