Tuesday 13 March 2012

Boxing and Unboxing in c#


//Program to Demonstrate Boxing And Unboxing
using System;
class Program
{
static void Main()
{
int i=123; // Value Type

object obj=i;// Implicit Boxing

int j=(int)obj;// Explicit/ Unboxing

//string s=(string)obj;//Note: This Statement will Give System.InvalidCastException
//why because object is boxed an int value while we r trying to convert it in to string

Console.WriteLine(j);
}
}

No comments:

Post a Comment