Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

blog
Ottorino Bruni  

Boxing and Unboxing in C#

C# is, for the most part, a statically typed language, this means that the compiler will check the type of every expression and you sometimes have to convert between types. The concept of boxing and unboxing is the starting point in C# type system in which a value of any type can be treated as an object.

As mentioned in my previous article C# Difference between Struct and Class, the important difference between a value type and a reference type is that the value type stores its value directly. A reference type stores a reference that points to an object on the heap that contains the value.

Boxing and Unboxing in C#

Boxing

Boxing is the process of taking a value type, putting it inside a new object on the heap and storing a reference to it on the stack. Boxing is an implicit conversion.

public class Program
{
    public static void Main(string[] args)
    {
        int total = 1976;
        object obj = total; // Boxing

        total = 2020;  

        System.Console.WriteLine("The value-type value = {0}", total); 
        System.Console.WriteLine("The reference-type value = {0}", obj); 
    } 
} 
/* Output: 
    The value-type value = 2020 
    The reference-type value = 1976 
*/

Unboxing

Unboxing is the exact opposite, it takes the item from the heap and returns a value type that contains the value from the heap. Unboxing is an explicit conversion. Attempting to unbox null or to unbox a reference to an incompatible value type causes an Exception.

public class Program
{
    public static void Main(string[] args)
    {
        int total = 1976;
        object obj = total;  // Boxing
        
        total = (int)obj; // Unboxing
        System.Console.WriteLine("The value-type value = {0}", total); 
        System.Console.WriteLine("The reference-type value = {0}", obj); 
    } 
} 
/* Output: 
    The value-type value = 1976 
    The reference-type value = 1976 
*/

Performance

There are some performance implications and memory requirements with each box and unboxing operation. When using the non-generic collections to store a value type, the boxing and unboxing operations can hurt performance.

Thanks for reading! 

🚀 Discover CodeSwissKnife, your all-in-one, offline toolkit for developers!

Click to explore CodeSwissKnife 👉

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.