Hi,
What is concept of Boxing and Unboxing ?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Posted Jul 6, 2012, 1:59 AM EDT
Simply, converting int value to object.
The object value will be stored into heap.
Ex :
int i = 10;
object o = i;
Unboxing is reverse of boxing. Converting reference object to value type.
Converting an object value to int value. Here you need to do manual type casting.
Value will be stored in stack.
Ex:
object o =10;
int i = (int)o;
Satyapriya NayakRank #47 · 31.5K reputationPosted Jul 6, 2012, 2:02 AM EDT
Boxing:
The conversion of a value type instance to an object, which implies that the instance will carry full type information at run time and will be allocated in the heap. The Microsoft intermediate language (MSIL) instruction set's box instruction converts a value type to an object by making a copy of the value type and embedding it in a newly allocated object.
Un-Boxing:
The conversion of an object instance to a value type.
Boxing :- Boxing is an implicit conversion of a value type to the type object type
Eg:-
Consider the following declaration of a value-type variable:
int i = 123;
object o = (object) i;
UnBoxing :- Unboxing is an explicit conversion from the type object to a value type
Eg:
int i = 123; // A value type
object box = i; // Boxing
int j = (int)box; // Unboxing
Thanks
Dehnert Stephen14 reputationPosted Jul 7, 2012, 8:32 AM EDT
It is converting Primitive datatype to object and vice-versa.
very simple integer to string object -- boxing
stringobject to int -- unboxing.
It can be seen in the ItemDataBound Event of a grid with itemtemplates.
In order to get the value first we will get the control and unbox it with the related control object say either a label/textbox/dropdown and then converting the value into string or a text is again boxing
Thanks
Buy Lace
VulpesRank #55 · 29.2K reputationPosted Jul 7, 2012, 9:39 AM EDT
For example:
void SomeMethod()
{
}
Boxing occurs because variables of interface types must always refer to an object on the heap.