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

Boxing and Unboxing in C#

achraf by achraf
August 29, 2020
in Blog, c#
0
Boxing and Unboxing in C#
0
SHARES
97
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

Auto Post and Schedule Tweets & Linkedin using Azure Logic Apps
Azure

Auto Post and Schedule Tweets & Linkedin using Azure Logic Apps

April 14, 2021
33
Migrate and modernize your applications on Azure

Migrate and modernize your applications on Azure – Part 2.0 (Proxy Function)

April 3, 2021
25
Migrate and modernize your applications on Azure
Azure

Migrate and modernize your applications on Azure – Part 2.0 (Azure Functions)

April 3, 2021
12
Migrate and modernize your applications on Azure – Part –1 (Migrate Database)
Azure

Migrate and modernize your applications on Azure – Part –1 (Migrate Database)

April 3, 2021
7
Migrate and modernize your applications on Azure
Azure

Migrate and modernize your applications on Azure – Part –1 (Create and publish Web App)

April 3, 2021
8
Migrate and modernize your applications on Azure
Azure

Migrate and modernize your applications on Azure – Part – 00 (creating .Net 5.0 application )

March 29, 2021
9
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 *

Recommended

Migrate and modernize your applications on Azure

Migrate and modernize your applications on Azure – Part 2.0 (Proxy Function)

April 3, 2021
25
Migration from Asp.Net Core 3.1 to 5.0 and publish to azure

Migration from Asp.Net Core 3.1 to 5.0 and publish to azure

November 12, 2020
123
Azure Policy for governance

Azure Policy for governance

August 29, 2020
171
The easiest way to deploy a website to Azure with Azure App Service

The easiest way to deploy a website to Azure with Azure App Service

April 21, 2020
128
Access Microsoft Azure Courses On Pluralsight for Free

Access Microsoft Azure Courses On Pluralsight for Free

April 21, 2020
76
How To Send Mail Using SQL Server

How To Send Mail Using SQL Server

April 20, 2020
39
Facebook Twitter LinkedIn Youtube
Auto Post and Schedule Tweets & Linkedin using Azure Logic Apps

Auto Post and Schedule Tweets & Linkedin using Azure Logic Apps

April 14, 2021
Migrate and modernize your applications on Azure

Migrate and modernize your applications on Azure – Part 2.0 (Proxy Function)

April 3, 2021
Migrate and modernize your applications on Azure

Migrate and modernize your applications on Azure – Part 2.0 (Azure Functions)

April 3, 2021

Categories

  • Apps (1)
  • Azure (25)
  • blazor (2)
  • Blog (43)
  • c# (6)
  • Cloud (24)
  • docker (2)
  • Games (1)
  • General Tips & Fix (1)
  • motivation (1)
  • Motivation (3)
  • News (7)
  • sql (3)
  • Tricks, Tips and Fixes (1)
  • xamarin (5)
No Result
View All Result
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • About