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 type parameters. The type parameter acts as a placeholder for a future type. When a generic type is declared, type arguments are substituted for the type parameters. At that time, the generic becomes type-specific, thus resolving many of the problems inherent in non-generic collection types. Generic methods, like generic types, have type parameters. However, the type argument can be inferred from the way the method is called.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.186.202