C# Program to Perform Unboxing Operation
Posted by Superadmin on August 11 2022 07:25:38

C# Program to Perform Unboxing Operation

 

This is a C# Program to perform unboxing operation.

Problem Description

This C# Program Performs Unboxing Operation.

Problem Solution

Here Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface.

Program/Source Code

Here is source code of the C# Program to Perform Unboxing Operation. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform Unboxing Operation
 */
using System;
class sample
{
    int data;
    void insert(object x)
    {
        data = (int)x * 5;
    }
    object delete()
    {
        data=0;
        return (object)data;
    }
    public static void Main()
    {
        sample s = new sample();
        s.insert(10);
        Console.WriteLine("Data : {0}", s.data);
        Console.WriteLine("Data : {0}", s.delete());
        Console.ReadLine();
    }
}
Program Explanation

In this C# Program, we are performing the unboxing operation. In sample class we have created insert() and delete() function. The insert() function use data variable to multiply the value of argument with the value of 5.

 

Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. Using object variable‘s’ we are passing the value 10 to the insert() function as an argument.

Runtime Test Cases
 
Data : 50
Data : 0