How to use Boxing and Unboxing in an application with code snippet?

Tripati_tutu
Posted by Tripati_tutu under C# category on | Points: 40 | Views : 1923
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoxingUnboxing
{
class Program
{
static void Main(string[] args)
{
int x = 10;
object obj;
obj = x; // implicit boxing
object obj1;
obj1 = (object)x; // explicit boxing
object obj2 = 20;
int y;
y = obj2; // This is invalid.
y = (int)obj2; // explicit unboxing
Console.WriteLine(obj + " " + obj1 + " " + obj2);
Console.WriteLine(x + " " + y);
Console.ReadKey();
}
}
}


You cannot perform unboxing implicitly. In the above example I have shown one statement as invalid.

Comments or Responses

Login to post response