Write A Program To Demonstrate Boxing And Unboxing In C#

Do you want to know about the boxing and unboxing in C#? In this C# tutorial, I will explain how to write a program to demonstrate boxing and unboxing in C#.

Boxing in C# is the process of converting a value type (like int or char) into a reference type (object), which allows value types to be treated as objects, particularly useful in collections. Unboxing is the reverse process, where a boxed object is converted back into a value type, often involving explicit casting. These operations are crucial for managing types in C#, but should be used judiciously to avoid performance costs.

What is Boxing in C#?

Boxing is the process of converting a value type (such as int, char, double) into a reference type, specifically into an object or to any interface type implemented by this value type. When a value type is boxed, a new object is allocated on the heap and the value is copied into it.

Why is Boxing Used?

  • Compatibility: Boxing allows value types to be treated as objects.
  • Collections: Before generics in .NET 2.0, collections like ArrayList could only store objects. Boxing enabled storing value types in these collections.

What is Unboxing in C#?

Unboxing is the reverse process of boxing. It converts a boxed object back into a value type. This operation involves explicit casting. Unboxing checks the object instance to ensure it’s a boxed value of the correct data type, and then extracts the value.

When is Unboxing Necessary?

  • Performance: To retrieve the value type from an object for operations requiring value type semantics.
  • Collections: When extracting value types stored in collections as objects.

Write A Program To Demonstrate Boxing And Unboxing In C#

Here is a complete example that will demonstrate boxing and unboxing in C#.

using System;

class BoxingUnboxingExample
{
    static void Main()
    {
        // Boxing
        int valType = 2023;
        object objType = valType; // The int is boxed into an object
        Console.WriteLine("Boxing: " + objType);

        // Unboxing
        int unboxedValType = (int)objType; // The object is unboxed back into an int
        Console.WriteLine("Unboxing: " + unboxedValType);
    }
}

In this code:

  1. Boxing: We declare an integer valType and then assign it to an object objType. This assignment triggers boxing, where valType is wrapped inside an object and stored on the heap.
  2. Unboxing: To retrieve the original value, we cast objType back to int. This casting triggers unboxing, extracting the value from the heap and storing it back in the stack as unboxedValType.

Once you run the code, you can see the output in the screenshot below.

write a program to demonstrate boxing and unboxing in c#

Conclusion

Understanding boxing and unboxing in C# is crucial for writing efficient and effective code. While these operations provide flexibility in handling different types of data, they also come with a performance cost. Excessive boxing and unboxing can lead to increased memory usage and slower performance. In this C# tutorial, I have explained how to write a program to demonstrate boxing and unboxing in C#.

You may also like: