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
Define managed code and managed data in .net?
What is asp net application object?
What is asp.net ajax?
How do you declare delegates and are delegates and events one and the same and explain how do you declare delegates and invoke them ?
What is viewstate information stored?
Define tracing.
Which method has beenintroduced in asp.net 4.0 to redirect a page permanently?
How would you enable impersonation in the web.config file?
Are cookies stored on server or client?
What is tracing? Where is it used?
What is the compiled object?
What is the usage of DelegatingHandler?
Where are session variables stored?
Is data edited in the Repeater control?
What is app_code folder in asp net?