Tuesday, August 29, 2017

2. Write a Program in C# to demonstrate boxing and Unboxing.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace boxing
{
class Program
{
static void Main(string[] args)
{
int m = 10;
object a = m;
try
{
Console.WriteLine("Value of m is :" + a);
object n = 20;
int b = (int)n;
Console.WriteLine("Value of n is :" + b);
System.Console.WriteLine("Boxing and unboxing is done");
Console.ReadLine();
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("Error Incorrect unboxing" + e.Message);
}
}
}
}
//converting from value type to reference type is known as BOXING
//converting from reference type to value type is known as UNBOXING

No comments:

Post a Comment