The source code to demonstrate the unboxing in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//Program to demonstrate the unboxing in C#
using System;
class UnBoxDemo
{
int intVar;
void Unbox(object Ob)
{
intVar= (int)Ob;
}
object Box(int val)
{
intVar = 0;
return (object)val;
}
public static void Main()
{
UnBoxDemo D = new UnBoxDemo();
object ObVal=10;
D.Unbox(ObVal);
Console.WriteLine("intVar : "+D.intVar);
ObVal = D.Box(20);
Console.WriteLine("ObVal : "+ObVal);
}
}
Output:
intVar : 10
ObVal : 20
Press any key to continue . . .
Explanation:
In the above program, we created a class UnBoxDemo that contains a data member intVar of integer type, and we also created two methods Box() and UnBox() that performs boxing and un-boxing respectively.
In the Main() method, we created the object D of UnBoxDemo class and then perform Unboxing and Boxing and print the values on the console screen.
Program:
The source code to demonstrate the unboxing in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created a class UnBoxDemo that contains a data member intVar of integer type, and we also created two methods Box() and UnBox() that performs boxing and un-boxing respectively.
In the Main() method, we created the object D of UnBoxDemo class and then perform Unboxing and Boxing and print the values on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer