Why do we need boxing and unboxing in C#?
I know what boxing and unboxing is, but I can’t comprehend the real use of it. Why and where should I use it?
short s = 25;
object objshort = s; //Boxing
short anothershort = (short)objshort; //Unboxing
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To have a unified type system and allow value types to have a completely different representation of their underlying data from the way that reference types represent their underlying data (e.g., an
intis just a bucket of thirty-two bits which is completely different than a reference type).Think of it like this. You have a variable
oof typeobject. And now you have anintand you want to put it intoo.ois a reference to something somewhere, and theintis emphatically not a reference to something somewhere (after all, it’s just a number). So, what you do is this: you make a newobjectthat can store theintand then you assign a reference to that object too. We call this process “boxing.”So, if you don’t care about having a unified type system (i.e., reference types and value types have very different representations and you don’t want a common way to “represent” the two) then you don’t need boxing. If you don’t care about having
intrepresent their underlying value (i.e., instead haveintbe reference types too and just store a reference to their underlying value) then you don’t need boxing.For example, the old collection type
ArrayListonly eatsobjects. That is, it only stores references to somethings that live somewhere. Without boxing you cannot put anintinto such a collection. But with boxing, you can.Now, in the days of generics you don’t really need this and can generally go merrily along without thinking about the issue. But there are a few caveats to be aware of:
This is correct:
This is not:
Instead you must do this:
First we have to explicitly unbox the
double((double)o) and then cast that to anint.What is the result of the following:
Think about it for a second before going on to the next sentence.
If you said
TrueandFalsegreat! Wait, what? That’s because==on reference types uses reference-equality which checks if the references are equal, not if the underlying values are equal. This is a dangerously easy mistake to make. Perhaps even more subtlewill also print
False!Better to say:
which will then, thankfully, print
True.One last subtlety:
What is the output? It depends! If
Pointis astructthen the output is1but ifPointis aclassthen the output is2! A boxing conversion makes a copy of the value being boxed explaining the difference in behavior.