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
What are standard controls?
Is there any alternative to avoid name collisions other then Namespaces?
Explain State management in asp.net
Explain parts of assembly?
What do you mean by role-based security?
What is a proxy in web service?
What does postback mean?
What is asp net theme?
Define the term Scavenging in Caching?
Describe SOA and the tenets of it?
Explain asp.net mvc request life cycle? : asp.net mvc
How can you register a custom server control to a web page?
What is a postback url?
Do session use cookies in asp net?
Explain the use of view state?