Skip to main content


Professional .NET 2.0 Generics

Tod Golding

ISBN: 978-0-471-75769-6 October 2005 386 Pages


The power and elegance of generic types have long been acknowledged. Generics allow developers to parameterize data types much like you would parameterize a method. This brings a new dimension of reusability to your types without compromising expressiveness, type-safety, or efficiency. Now .NET generics makes this power available to all .NET developers. By introducing generic concepts directly into the Common Language Runtime (CLR), Microsoft has also created the first language-independent generics implementation. The result is a solution that allows generic types to be leveraged by all the languages of the .NET platform.

This book explores all aspects of the .NET generics implementation, covering everything from fundamental generic concepts, to the elements of generic syntax, to a broader view of how and when you might apply generics. It digs into the details associated with creating and consuming your own generic classes, structures, methods, delegates, and interfaces, examining all the nuances associated with leveraging each of these language constructs. The book also looks at guidelines for working with generic types, the performance gains achieved with generics, the new generic container libraries (BCL and third party), and key aspects of the underlying .NET implementation.

For those transitioning from C++, the book provides an in-depth look at the similarities and differences between templates and.NET generics. It also explores the syntactic variations associated with using generics with each of the .NET languages, including C#, Visual Basic, J#, and C++.



Chapter 1: Generics 101.

Chapter 2: Valuing Type Safety.

Chapter 3: Generics ≠ Templates.

Chapter 4: Generic Classes.

Chapter 5: Generic Methods.

Chapter 6: Generic Delegates.

Chapter 7: Generic Constraints.

Chapter 8: BCL Generics.

Chapter 9: Reflection, Serialization, and Remoting.

Chapter 10: Generics Guidelines.

Chapter 11: Under the Hood.

Chapter 12: Using Generics with C++.

Chapter 13: Using Generics with J#.

Chapter 14: Power Collections.


Download the source code.
Code downloads for this title are available here.
ChapterPageDetailsDatePrint Run
5Error in Code
first block of code, line below the C# heading:

SalesPerson aSalesPerson = (SalesPerson)child[0].Data;

should be:

SalesPerson aSalesPerson = (SalesPerson)child1[0].Data;

225Error in Text
There is no need for explicit casting in the line below:
Object[] orders = (Object[])cust.Items;

should be:
Object[] orders = cust.Items

31Error in Code
2nd and 4th shaded code blocks, under each [C# code] heading:
1st line of each:

should be:

31Error in Code
2nd and 4th shaded code blocks, under each [C# code] heading:
2nd line of each, in Public:

should be:

51Error in Code
code block, VB code, 5th line:


should be:


58Error in Code
In the VB Code example (top shaded code block), 4th line from end:
Dim cache2 As New MyCache(Of String, String)()

should be:
Dim cache2 As New MyCache(Of String, Integer)()

102Error in Code
2nd and 4th lines of shaded code on the page (both C# lines):
the ending section of both lines in parens:
(Byval val As Order)

should be:
(Order val)

116Error in Code
Page 116, 2nd shaded code section (C#), lines 9 and 10

Code reads:

public bool Update() {
return false;

Should be:

public void Update() {