Generics C# Help

A new feature of the CLR 2.0 is the introduction of generics. With CLR 1.0, creating a flexible class or method that should use classes that are not known at compile time must be based on the Object. class. With the Object class, there’s no type safety during compile time. Casting is necessary. Also, using the Object class for value types has a performance impact.

CLR 2.0 (.NET 3.5 is based on the CLR 2.0) supports generics. With generics, the Object class is no longer necessary in such scenarios. Generic classes make use of generic types that are replaced with specific types as needed. This allows for type safety: the compiler complains if a specific type is not supported with the generic class.

Generics are ~great feature, especially with collection classes. Most of the .NET 1.0 collection . classes are based on the Object type. Starting with version 2.0, .NET offers collection classes that are implemented as generics.

Generics are not limited to classes; in this chapter, you also see generics with delegates, interfaces, and methods.

This chapter discusses the following:

  1.  Generics overview
  2.  Creating.generic classes
  3.  Features of generic classes
  4.  Generic interfaces
  5.  Generic methods
  6.  Generic delegates
  7.  Other generic framework types.

Posted on October 29, 2015 in Generics

Share the Story

Back to Top