Introducing Generics

Generic types are .NET types that can adapt their behavior to different kind of objects without the need of defining a separate version of the type. In other words, you can implement an only generic type to work with integers, strings, custom reference types, and any other .NET type with a single implementation. Generics offer several advantages:

Strongly typed programming techniques: Generic objects can hold only the specified type and avoid accidents of handling objects of different types within the same group.

Better performances: Because Generics enable handling only the specified type, they avoid the need of boxing and unboxing from and to System.Object, and this retains for performance overhead.

Code reuse: As you will better see in a few moments, Generics enable creating objects that behave the same way and that have the same infrastructure whatever kind of .NET type you pass them.

The ability of writing better code: Avoiding working with nongeneric System.Object, you not only get all IntelliSense features for the specified type, but you also take advantages from not using late-binding techniques.

Generally the use of Generics is related to the creation of strongly typed collections for storing groups of items of the same type. Because of this, there are two considerations: the first one is that you should check if the .NET Framework provides a built-in generic collection suiting your needs before creating custom ones (read Chapter 16); the second consideration is that code examples shown in this chapter will be related to creating a generic collection so that you know how to create one if .NET built-in generics are not sufficient for you.

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

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