Generics
Chapter 7 is about generics, which are templated collections. Generics improve upon non-generic collections. Generics improve performance mostly by eliminating boxing. They are bound to specific types at compile time. Non-generic collections rely on object types. When populating non-generic collections with values, boxing occurs because of casting from a value type to a reference type. Excessive boxing incurs both a performance and a memory penalty. Boxing also causes problems with type safety. You often cast elements that are object types to a specific type. The cast is not type-safe and can cause runtime errors when done incorrectly. Because generics are type-specific, the need for a cast is eliminated.
Generic types are classes with ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access