.NET Boxing vs Unboxing for Dummies

The memory palace is a powerful technique that can help programmers memorize concepts and never have to mess them up.

Mihai Sandu
Geek Culture

--

©Mihai Sandu

Boxing and Unboxing stay at the base of C# programming. You bump into them when calling functions, creating objects, at interviews, and so on. They are ubiquitous in the .NET space, so a solid understanding will improve your skills. Let’s explore these concepts and how they are related to stack and heap memory.

First, this is the technical explanation, straight from Microsoft:

“Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type […] Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. […] Boxing a value type allocates an object instance on the heap.

Have you understood that? Great! Now go drink a glass of water (it’s important to stay hydrated) and by the time you’ll be back you’ll have already forgotten which is which.

As programmers, we have to remember a lot of concepts, and memorization by repetition is not the answer. We have to be logical. Or tell stories.

The memory palace is an ancient memorization technique. It’s how memory champions remember up…

--

--

Mihai Sandu
Geek Culture

A software developer interested in writing about programming, technology, environment, and self-development. Twitter @mihais77