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.
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
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
Thanks for checking out the podcast. We’d love to hear your honest feedback in your iTunes review or at firstname.lastname@example.org.