What is boxing and unboxing ?

Answer Posted / narayansahu

Boxing and Un-boxing

Boxing allows value types to be implicitly treated like
objects.

Dim i As Integer = 5
i.ToString() ' i --> object --->object.tostring

OR

Dim i As Integer = 5
Dim obj As Object = i ' implicit boxing
obj.ToString()

OR you can do it explicitly
Dim obj As Object = CType(i, Object) ' un-necessary
explicit boxing

On the other hand, un-boxing is an explicit conversion from
object type to value type.

Dim i As Integer = 5
Dim obj As Object = i ' implicit boxing
Dim j As Integer = CType(obj, Integer) ' explicit un-boxing



? Boxing and Un-boxing is the transformation between
value type and object type while casting just transforms
the apparent (reference) type of objects.
? Value types are stored at the stack and objects are
stored at the heap.
Boxing takes a copy of value types from the stack to the
heap while un-boxing takes value types back to the stack.
On the other hand, casting does not physically move or
operate on an object. Casting merely changes the way
objects are treated in a program by altering their
reference type.

Is This Answer Correct ?    23 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by authentication?

528


What is Web API Routing?

607


Explain the difference between Web Garden and Web Farm?

551


How to do state management in ASP.NET?

601


What is application Object?

595






Explain the difference between response.redirect vs server.transfer

618


Why do we need master page in asp.net?

532


How to prevent client side validation from the ASP.NET validation controls?

579


What is a viewstate?

570


what are the events raised in asp.net page life cycle?in which stage view state can be loaded?

557


What is _viewstart?

508


What is the difference between login controls and forms authentication?

546


What is OSI layer? Explain different layers.

607


What is user control in asp.net?

512


Explain the various authentication mechanisms in asp.net.

493