Audio Player

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 | 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 or 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 vs CLR via C#

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

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

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 or tweeting there.