How To Use Boxing And Unboxing in C#

In this article, we will learn how to use boxing and unboxing in C#

Boxing

A boxing conversion permits any value-type to be implicitly converted to the type object or to any interface-type implemented by the value-type. Boxing a value of a value-type consists of allocating an object instance and copying the value-type value into that instance.

First of all creating a box of int type (10 bytes) and then creating an object “om” and allocating memory to it.

int i = 12;  
object box = i;

conceptually correspond to,

int i = 12;  
object box = new int_Box(i);

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DEMOAPP
{
    class box
    {
        public static int A
        {
            get;
            set;
        }
        public static void Main()
        {

            // -- Statement for unboxing –

            int i = 5;
            object j = i;

            // ----------------------------------------

            // -- Statement for unboxing --
            //int i = 5;
            //object j = i;
            //i = (int)j;

            // ----------------------------------------

            Console.WriteLine("Enter val of A: ", A);
            a = Convert.ToInt32(Console.ReadLine());

            if (i <= A)
            {
                i += A;
                Console.WriteLine("\n Sum is: {0}", i);
            }
            else
            {
                Console.WriteLine("\n Your stucked!");
            }
            Console.ReadLine();
        }
    }
}

Unboxing

An unboxing conversion permits an explicit conversion from type object to any value-type or from any interface-type to any value-type that implements the interface-type. An unboxing operation consists of first checking that the object instance is a boxed value of the given value-type, and then copying the value out of the instance.

object box = 12;  
int i = (int)box;

conceptually correspond to,

object box = new int_Box(12);  
int i = ((int_Box)box).value;

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DEMO
{
    class box
    {
        public static int k
        {
           (ing properties to the metho)
            get;
            set;
        }
        public static void Main()
        {

            // -- Statement for unboxing –

            //int i = 5;
            //object j = i;

            // ----------------------------------------

            // -- Statement for unboxing --
            int i = 5;
            object j = i;
            i = (int)j;

            // ----------------------------------------

            Console.WriteLine("Enter val of k: ", k);
            k = Convert.ToInt32(Console.ReadLine());

            if (i <= k)
            {
                i += k;
                Console.WriteLine("\n Sum is: {0}", i);
            }
            else
            {
                Console.WriteLine("\n Your stucked!");
            }
            Console.ReadLine();
        }
    }
}

Hope you understand the article , If you still have any questions or queries then please let me know in the comment section, I’ll respond to you as soon as possible.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories