Boxing--> It is a process of converting a variable from value type to reference type. it supports two types,
1. Implicit boxing
2. Explicit Boxing
Boxing is 20 times costlier than normal Initialization.
UnBoxing--> Converting a variable from Reference type to value type. it supports only One type,
1. Explicit Boxing
UnBoxing is 4 times Costlier than normal Initialization.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace box
- {
- class Program
- {
- static void Main(string[] args)
- {
- int i = 10;
- object O = i;
- object X = (object) i;
- int j = (int) O;
- Console.WriteLine("value of i is:-" + i);
- Console.WriteLine("value of O after Implicit Boxing is :-" + O);
- Console.WriteLine("value of X after Explicit Boxing is :-" + X);
- Console.WriteLine("value of j after Explicit UnBoxing is :-" + j);
- Console.Read();
- }
- }
- }