Achraf Ben Alaya
No Result
View All Result
  • Home
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
    • General Tips & Fix
  • AI
  • Cloud
  • Motivation
  • Courses
  • About
    • Resume
    • Privacy Policy
SUBSCRIBE
  • Home
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
    • General Tips & Fix
  • AI
  • Cloud
  • Motivation
  • Courses
  • About
    • Resume
    • Privacy Policy
No Result
View All Result
Achraf Ben Alaya
No Result
View All Result
ADVERTISEMENT
Home Blog

Boxing and Unboxing in C#

achraf by achraf
August 29, 2020
in Blog, c#
2 min read
0
Boxing and Unboxing in C#
0
SHARES
1.1k
VIEWS
Share on FacebookShare on Twitter

As part of graduation projects , each year we should have internships in summer , so we had to prepare ourselves for interviews for technical and non technical question . I remember in one of the interviews , the last question was ,’can you explain to me what is boxing and unboxing ? ā€˜ and I was like :

Well , I stood like that for 5 minutes and ended up saying that I don’t know the answer , then the interviewer get up from his desk and explained it to me by writing on a blackboard . When he was explaining I was saying in my mind : ā€˜Dude ,I have been doing that all my life , I didn’t know it’s called boxing and unboxing ! ā€˜ .

Well , that was an interview that didn’t go well , but at least I learned something from it which encouraged me to write about it now .

What is Boxing And Unboxing ? Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type.

So when we say boxing,we say it’s 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 . Unboxing is the exact opposite.

The 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. so : Unboxing extracts the value type from the object. Boxing is implicit and unboxing is explicit.

Let’s start by a simple boxing example :

int i = 123456;

// The following line boxes i inside object o. object o = i; The object o now, can be unboxed and assigned to integer variable i

o = 123; i = (int)o;

// unboxing o Let’s see one more example , so we have the following code :

class TestUnboxing

class TestUnboxing 
{ static void Main() { int i = 123; object o = i; // implicit boxing

    try
    {
        int j = (short)o;  // attempt to unbox

        System.Console.WriteLine("Unboxing OK.");
    }
    catch (System.InvalidCastException e)
    {
        System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message);
    }
}
}

 

So the output of this code if you try to execute it is , ā€ Specified cast is not valid. Error: Incorrect unboxing. ā€ and why is that ?

As you can see , we have defined i as int , but when we tried to unbox it we used short which cause the exception here , so what we need to do is :

int j = (int) o;
A picture that can explain all that :

Performance In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally. For more information, see Performance in Microsoft official website .

Happy boxing && unboxing day šŸ™‚

 

ShareTweet
Previous Post

Achraf First Post

Next Post

Sql tips and tricks

Related Posts

AI

Build and Host an Expense Tracking MCP Server with Azure Functions

November 2, 2025
28
Azure

Log Analytics Workspace Chaos: How We Tamed 100+ Orphaned Workspaces

October 17, 2025
143
Azure

Honored to be recognized as a Microsoft Azure MVP for 2025-2026

July 20, 2025
119
AI

Model Context Protocol (MCP): The Future of AI Integration

April 21, 2025
273
Azure

Step-by-Step Guide: Azure Front Door + Storage Account Static Website + Custom Domain with Terraform

March 11, 2025
872
Network Security & Route Tables – Checking NSGs, route tables, and service endpoints for a targeted VNET or Subnet
Azure

Network Security & Route Tables – Checking NSGs, route tables, and service endpoints for a targeted VNET or Subnet

February 3, 2025
208
Next Post
Sql tips and tricks

Sql tips and tricks

Leave a Reply Cancel reply

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

Terraform

Certifications

Microsoft certified trainer (MCT)

Recommended

Azure Tips

Azure Tips

April 28, 2020
468
Finally a stable version of Xamarin.Forms 4.1.0 and announcing Xamarin.Essentials 1.2

Finally a stable version of Xamarin.Forms 4.1.0 and announcing Xamarin.Essentials 1.2

April 21, 2020
711
Streamlining Website Editing on My Local Machine with Docker Compose and WordPress

Streamlining Website Editing on My Local Machine with Docker Compose and WordPress

July 1, 2023
175
Access Microsoft Azure Courses On Pluralsight for Free

Access Microsoft Azure Courses On Pluralsight for Free

April 21, 2020
498
How to SSH into AKS Nodes

How to SSH into AKS Nodes

May 11, 2021
7.8k
PowerShell Automation for Azure Networks: Detailed VNET and Subnet Analysis

PowerShell Automation for Azure Networks: Detailed VNET and Subnet Analysis

November 2, 2024
760
Facebook Twitter LinkedIn Youtube

Build and Host an Expense Tracking MCP Server with Azure Functions

November 2, 2025

Log Analytics Workspace Chaos: How We Tamed 100+ Orphaned Workspaces

October 17, 2025

Honored to be recognized as a Microsoft Azure MVP for 2025-2026

July 20, 2025

Categories

  • AI (3)
  • Apps (1)
  • Azure (66)
  • blazor (2)
  • Blog (92)
  • c# (7)
  • Cloud (67)
  • Courses (4)
  • Dapr (4)
  • docker (4)
  • Games (1)
  • General Tips & Fix (1)
  • Home (1)
  • Kubernetes Service (AKS) (1)
  • motivation (2)
  • Motivation (3)
  • News (9)
  • Resume (1)
  • sql (4)
  • Terrafrom (1)
  • Tricks, Tips and Fixes (4)
  • xamarin (5)
No Result
View All Result
  • Home
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
    • General Tips & Fix
  • AI
  • Cloud
  • Motivation
  • Courses
  • About
    • Resume
    • Privacy Policy