C# Boxing

C# Code Snippets Boxing


In this code snippet, we will take a look at boxing and unboxing in C#.

Boxing happens when a value data type is converted to an object while unboxing is when that object gets converted back into a value.

This should be avoided because the objects get created on the heap which slows things down.

Let’s have a look at the code below to see how boxing works.


int myValue = 0;
object myObject = myValue; //myValue gets boxed.
int myOtherValue = (int)myObject; //myValue gets unboxed.

Leave a Reply

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

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger