Boxing and unboxing
Boxing and unboxing is an important concept in C# type system. With Boxing and unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object.
Boxing
-
Boxing is a mechanism in which value type is converted into reference type.
-
It is implicit conversion process in which object type (super type) is used.
-
In this process type and value both are stored in object type
Unboxing
-
Unboxing is a mechanism in which reference type is converted into value.
-
It is explicit conversion process.
Program to show boxing and unboxing:
using System;
namespace boxing
{
class Program
{
static void Main(string[] args)
{
int i = 10;
object o = i; // boxing
int j = (int)o; // unboxing
Console.WriteLine("value of o object : " + o);
Console.WriteLine("Value of j : " + j);
Console.ReadLine();
}
}
}
Boxing is a mechanism in which value type is converted into reference type.
It is implicit conversion process in which object type (super type) is used.
In this process type and value both are stored in object type
Unboxing is a mechanism in which reference type is converted into value.
It is explicit conversion process.
No comments:
Post a Comment