Hi,
What is concept of Boxing and Unboxing ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 7, 2012, 1:39 PM
For example:
void SomeMethod()
{
}
Boxing occurs because variables of interface types must always refer to an object on the heap.
Dehnert StephenPosted Jul 7, 2012, 12:32 PM
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
Satyapriya NayakPosted Jul 6, 2012, 6:02 AM
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
Posted Jul 6, 2012, 5:59 AM
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;