What is Boxing and Unboxing?

Answer Posted / kumaresh

BOXING: Converting value type to referenc type.
Here we go with an example,

ArrayList instance = new ArrayList()
//In the above line of code,ArrayList is a reference
type.Add some items to the arraylist. For example,

instance.Add(1);
instance.Add(100);
instance.Add(3);
//3 items added to the arraylist.
//Integer valuee 3 is value type.Adding 3 to the reference
type(here it's arraylist) is boxing.
i.e., Converting value type to referenc type.

UNBOXING:Converting Reference type to value type.
Below is an example, retrives the items from the
arraylist.

foreach (int item in arrayList)
Console.WriteLine(item);

Here arraylist(referenec type) will have 3 values in the
list, which we added above.
Adding this arraylist items to an int variable[here int
variable is "item"] is unboxing.
i.e.,converting the reference type to value type.

Hope you would have understood.
Thanks

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of service provider?

586


What are the various session state management options provided by asp.net?

538


What is a response cookie?

606


What is difference between viewstate and session in asp net?

559


What are the types of session in asp.net?

525






How would you create a permanent cookie?

566


Why is this service branded with windows livetm?

607


How should I destroy my objects in asp.net?

597


From which base class all web forms are inherited?

497


What is ashx file in asp.net?

547


What are the main advantages of using asp.net?

552


What are the ways to show data grid inside a data grid for a master details type of tables? If we write any code for DataGrid methods, what is the access specifier used for that methods in the code behind file and why?

582


What is difference between singleton and single call?

644


Why would anyone need to implement their own hashtable or linked list?

2435


What do you mean by caching in asp.net?

548