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 an asp.net web form?

634


How can we secure the data which is send from client side to server? Like the login id and paasword needs to be authenticated on the server but we cannot send it in plain text into the server.One more thing we are not using the SSL here.

1426


What is http post action?

547


Where is the view state data stored in asp net?

563


Explain About duration in caching technique

623






Is web config mandatory?

540


What is the difference between the response.write() and response.output.write() methods?

520


How is application management and maintenance improved in asp.net 2.0?

543


What is asp according to you?

530


What is a web server? What are the load limits in it?

592


What is the difference between Session and response.Redirect?

620


Define msil.

551


Where is http session stored?

503


How do you secure your configuration files to be accessed remotely by unauthorized users?

627


Explain the difference between server control and html control.

479