Podcast: Play in new window | Download
Subscribe: Apple Podcasts | Google Podcasts | Spotify | Stitcher | TuneIn | RSS
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 to 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.
Thanks for listening!
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 takes time/cpu
- Implicit Boxing (It’s sneaky!)
- They’re (almost) unnecessary!
Boxing and unboxing is big, slow, ugly, sneaky, and almost completely unnecessary. Take a deeper look at 7 of the reasons why programmers beat up on boxing:
Boxing and Unboxing in C#: 7 Deadly Sins
Unintended consequences with interfaces
15:39 – 17:50
- Interfaces are reference types
- Intefaces with methods that take Objects must box ValueTypes
- IComparable.CompareTo(object obj) example
17:50 – 24:24
- Generic Interfaces
- Detecting boxing/unboxing with ILDasm and ILSpy
- ToString() Trick
Steps to Understanding Boxing and Unboxing
We’ve discovered a little trick that lets you bypass a boxing/unboxing operation in some situations by calling ToString:
Avoiding boxing in String and Console methods
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
Thanks for checking out the podcast. We’d love to hear your honest feedback in your iTunes review or at firstname.lastname@example.org.