Notes
Slide Show
Outline
1
Boxing
2
Type Recap
  • value types
    • variables contain their own data directly
    • local variables always live on the stack
  • reference types
    • variables refer to their data indirectly
    • local variables refer to objects on the heap
3
struct vs. class
4
Objects Everywhere
  • everything implicitly inherits from object
    • arrays, classes, interfaces, delegates
    • structs, enums, as well!
5
Problem
6
Boxed Solution
  • when a reference variable binds to a value
    • the runtime copies the value onto the heap
    • the reference refers to the copy on the heap
    • this is called boxing
7
Unboxing
  • unboxing copies the boxed value back again
    • this requires an explicit cast to the exact type
    • If the reference is null - NullReferenceException
    • if the target type is wrong - InvalidCastException
8
System.Object
  • boxing creates a unified type system
9
No Overhead
  • in fact, value types do not derive from object
    • each value type has a hidden wrapper type
    • the wrapper type holds the boxed value
    • object virtual methods aren't overridden in values
    • they are overridden in the wrapper type
10
Blatant Advert