Overloading Type Parameters

You can overload generic definitions providing different signatures for the type parameter, similar to what happens in methods overloads. The following code provides an example:

image

It is worth mentioning that providing a nongeneric version of your class is not necessary. In this way you can provide different implementations for your generic types. Now consider the following overloading attempt:

image

This code is not compiled because although in the second definition some constraints are defined, the type implementation is considered by the compiler with the same identical signature. Similarly you can provide overloaded methods using techniques learned in Chapter 7, “Class Fundamentals,” but against generic methods as demonstrated in the following code:

image

Overloading provides great granularity over Generics implementation, and you will often see examples in built-in generic collections.

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

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