Interview Question

What is boxing/unboxing?

Boxing wraps a copied value in an object; unboxing explicitly retrieves it.

💡 Concept ✅ Quick Revision 🔷 C#

Answer

Boxing converts a value type to object or to an interface type implemented by that value type. • Boxing creates an object containing a copy of the value. • Unboxing extracts the value through an explicit conversion to the matching value type. • Unboxing to an incompatible type throws InvalidCastException.

💡 C# Example

int number = 42; object boxed = number; int unboxed = (int)boxed; Console.WriteLine(unboxed);

Output

42

⚡ Quick Revision

Boxing wraps a copied value in an object; unboxing explicitly retrieves it.