### Audio Player

[http://media.blubrry.com/codingblocks/www.podtrac.com/pts/redirect.mp3/traffic.libsyn.com/codingblocks/coding-blocks-episode-002.mp3](http://media.blubrry.com/codingblocks/www.podtrac.com/pts/redirect.mp3/traffic.libsyn.com/codingblocks/coding-blocks-episode-002.mp3)

Podcast: [Play in new window](http://media.blubrry.com/codingblocks/www.podtrac.com/pts/redirect.mp3/traffic.libsyn.com/codingblocks/coding-blocks-episode-002.mp3 "Play in new window") | [Download](http://media.blubrry.com/codingblocks/www.podtrac.com/pts/redirect.mp3/traffic.libsyn.com/codingblocks/coding-blocks-episode-002.mp3 "Download")

This episode is all about boxing and unboxing. We discuss memory management, the pros (yes, there are a few!) and cons of boxing/unboxing, some of the weird side effects and how you can avoid it with generics and ToString methods.

Download the episode on [iTunes](http://bit.ly/cbnet-itunes) or [Stitcher](http://bit.ly/cbnet-stitcher) and make sure to drop your feedback on iTunes.

## Show Notes

Duration: ~33m

Can we make it through an episode without mentioning this book?

## Memory Management in .Net

0:00 – 10:39

- Grand Theft Example – Shipper doesn’t care what’s in the box!
- Value Types vs Reference Types
- Stack vs Heap
- Nullable Types are value types
- Boxing and Unboxing
- [Msdn](http://msdn.microsoft.com/en-us/library/b95fkada(v=vs.80).aspx) vs [CLR via C#](http://www.amazon.com/CLR-via-C-Developer-Reference/dp/0735667454/ref=sr_1_1?ie=UTF8&qid=1380490153&sr=8-1&keywords=clr+via+c%23)

## Boxing and Unboxing: 7 Deadly Sins

10:39 – 15:39

1. Boxed values take up more memory.
2. Boxed values require an additional read
3. Short-lived values clog the heap
4. Boxing and unboxing operations take time/cpu
5. Casting
6. Implicit Boxing (It’s sneaky!)
7. They’re (almost) unnecessary!

## Unintended consequences with interfaces

15:39 – 17:50

- Interfaces are reference types
- Interfaces with methods that take Objects must box ValueTypes
- IComparable.CompareTo(object obj) example

## Avoiding boxing

17:50 – 24:24

- Generic Interfaces
- Detecting boxing/unboxing with [ILDasm](http://msdn.microsoft.com/en-us/library/f7dy01k1.aspx) and [ILSpy](http://ilspy.net/)
- [ToString() Trick](http://ilspy.net/)

## In Defense of Boxing/Unboxing

24:24 – 27:49

- Pre .NET 2.0 code (ArrayList and HashTable)
- 3rd party libraries
- Dynamics and Reflection
- Any method that takes an object

## Tips & Tricks

27:49 – 33:38

- [Assign labels to your breakpoints](http://msdn.microsoft.com/en-us/library/vstudio/dd293674(v=vs.100).aspx)
- Visual Studio Multi Display
- [dotPeek – Free .NET decompiler](http://www.jetbrains.com/decompiler/)

#### About Joe Zack

Joe is a Software Developer based in Central Florida. If it weren't for ticketing systems he would gladly refactor himself in circles all day long.

These days Joe is excited about JAMstack and Search.

When he is not basking in the glow of a monitor you can find him walking around Cocoa Beach or annoying his wife by playing "music".

Joe can be found blogging [here](http://joezack.com/) or tweeting [there](https://twitter.com/thejoezack).
