C# Interview questions on Boxing and Unboxing

Q : What is Boxing and Unboxing?

Ans : Boxing - Converting a value type to reference type is called boxing. An example is shown below.
int i = 101;
object obj = (object)i; // Boxing

Unboxing - Converting a reference type to a value typpe is called unboxing. An example is shown below.
obj = 101;
i = (int)obj; // Unboxing

Q : Is boxing an implicit conversion?

Ans : Yes, boxing happens implicitly.

Q : Is unboxing an implicit conversion?

Ans : No, unboxing is an explicit conversion.

Q : What happens during the process of boxing?

Ans : Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object. Due to this boxing and unboxing can have performance impact.

Leave a Reply
Name (required)
Email (will not be published) (required)
Web Site

List of Top Hospitals in India