Audio Player
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
- Boxed values take up more memory.
- Boxed values require an additional read
- Short-lived values clog the heap
- Boxing and unboxing operations take time/cpu
- Casting
- Implicit Boxing (It’s sneaky!)
- 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 and ILSpy
- ToString() Trick
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
- Visual Studio Multi Display
- dotPeek – Free .NET 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".