Q:

C# program to demonstrate the example of unboxing

0

C# program to demonstrate the example of unboxing

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

//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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate the example of single in... >>
<< C# program to demonstrate the example of a sealed ...