O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Professional .NET 2.0 Generics

Book Description

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++.

Table of Contents

  1. Copyright
  2. About the Author
  3. Credits
  4. Acknowledgments
  5. Introduction
  6. Generics 101
    1. Why Generics?
    2. Terminology
    3. Summary
  7. Valuing Type Safety
    1. Motivation
    2. Least Common Denominator Programming
    3. A Basic Example
    4. Safety vs. Clarity
    5. Summary
  8. Generics ≠ Templates
    1. Shared Concepts
    2. Run-Time vs. Compile-Time
    3. Templates Extras
    4. Cross-Language Support
    5. Debugging
    6. Mix and Match Nirvana
    7. Summary
  9. Generic Classes
    1. Parameterizing Types
    2. Inheritance
    3. Fields
    4. Methods
    5. Nested Classes
    6. Consuming Generic Classes
    7. The Default Keyword
    8. System.Nullable<T>
    9. Accessing Type Info
    10. Indexers, Properties, and Events
    11. Generic Structs
    12. Generic Interfaces
    13. Summary
  10. Generic Methods
    1. The Basics
    2. A Deeper Look
    3. Apply Constraints
    4. Type Parameter Names
    5. Overloading Generic Methods
    6. Overriding Generic Methods
    7. Type Inference
    8. Generic Methods and Delegates
    9. Type-Safe Database Access Example
    10. Summary
  11. Generic Delegates
    1. Delegate Basics
    2. Adding Generics to the Equation
    3. Event Handling
    4. Generic Delegates with Generic Methods
    5. Delegates in the Framework
    6. Type Coercion
    7. Applying Constraints
    8. Delegates and Anonymous Methods
    9. Summary
  12. Generic Constraints
    1. Overview
    2. Constraint Types
    3. Boxing and Constraints
    4. Using Multiple Constraints
    5. Generic Delegate and Method Constraints
    6. Inheritance and Constraints
    7. Are Generics Generic?
    8. Summary
  13. BCL Generics
    1. Motivation
    2. The Big Picture
    3. Generic Collection Classes
    4. Testing Equality
    5. Null Collection Elements
    6. More to Come
    7. Summary
  14. Reflection, Serialization, and Remoting
    1. Reflection
    2. Serialization
    3. Remoting
    4. Summary
  15. Generics Guidelines
    1. An Evolving List
    2. Defining Guidelines
    3. Organization
    4. Identifying Generic Opportunities
    5. Balancing Readability with Expressiveness
    6. Using BCL Generic Types
    7. Applying Constraints
    8. The Kitchen Sink
    9. Summary
  16. Under the Hood
    1. Overview
    2. Assumptions
    3. High-Level Goals
    4. IL Representation of Generic Types
    5. Specialization and Sharing
    6. Exact Run-Time Types
    7. Support for Polymorphic Recursion
    8. NGen and Generic Types
    9. Performance
    10. Memory Footprint
    11. Backward Compatibility
    12. Summary
  17. Using Generics with C++
    1. Templates or Generics?
    2. Blurring the Lines
    3. No Limits
    4. Generic Classes in C++
    5. Generic Methods in C++
    6. Generic Interfaces in C++
    7. Constraints
    8. Generic Delegates in C++
    9. Mixing Generics and Templates
    10. STL.NET
    11. Summary
  18. Using Generics with J#
    1. J# Generic Limitations
    2. Migrating from Java Generics
    3. Consuming Generic Types
    4. Leveraging Cross-Language Support
    5. Working with Interfaces
    6. Complying with Constraints
    7. Calling Generic Methods
    8. Java Type Arguments
    9. Calling Generic Delegates
    10. Arrays of Generic Types
    11. Summary
  19. Power Collections
    1. A Little History
    2. The Big Picture
    3. Class Reference
    4. Additional Libraries
    5. Summary
  20. Index