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.