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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.