Summary

Section 20.1 Introduction

  • Generic methods enable you to specify, with a single method declaration, a set of related methods.

  • Generic classes enable you to specify, with a single class declaration, a set of related classes.

  • Generic interfaces enable you to specify, with a single interface declaration, a set of related interfaces.

  • Generics provide compile-time type safety.

Section 20.2 Motivation for Generic Methods

  • Overloaded methods are often used to perform similar operations on different types of data.

  • When the compiler encounters a method call, it attempts to locate a method declaration that has the same method name and parameters that match the argument types in the method call.

Section 20.3 Generic-Method Implementation

  • If the operations performed by several overloaded methods are identical for each argument type, the overloaded methods can be more compactly and conveniently coded using a generic method.

  • You can write a single generic-method declaration that can be called at different times with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately.

  • All generic-method declarations have a type-parameter list delimited by angle brackets that follows the method’s name. Each type-parameter list contains one or more type parameters, separated by commas.

  • A type parameter is used in place of actual type names. The type parameters can be used to declare the return type, parameter types and local variable types in a generic-method declaration; the type parameters act as placeholders for type arguments that represent the types of that data that will be passed to the generic method.

  • A generic method’s body is declared like that of any other method. The type-parameter names throughout the method declaration must match those declared in the type-parameter list.

  • A type parameter can be declared only once in the type-parameter list but can appear more than once in the method’s parameter list. Type-parameter names need not be unique among different generic methods.

  • When the compiler encounters a method call, it analyzes the set of methods (both nongeneric and generic) that might match the method call, looking for a method that best matches the call. If there are no matching methods, or if there’s more than one best match, the compiler generates an error.

  • You can use explicit type arguments to indicate the exact type that should be used to call a generic function. For example, the method call DisplayArray<int>(intArray); explicitly provides the type argument (int) that should be used to replace type parameter T in the DisplayArray method’s declaration.

  • For each variable declared with a type parameter, the compiler also determines whether the operations performed on such a variable are allowed for all types that the type parameter can assume.

Section 20.4 Type Constraints

  • Generic code is restricted to performing operations that are guaranteed to work for every possible type. Thus, an expression like variable1 < variable2 is not allowed unless the compiler can ensure that the operator < is provided for every type that will ever be used in the generic code. Similarly, you cannot call a method on a generic-type variable unless the compiler can ensure that all types that will ever be used in the generic code support that method.

  • It’s possible to compare two objects of the same type if that type implements the generic interface IComparable<T> (of namespace System), which declares method CompareTo.

  • IComparable<T> objects can be used with the sorting and searching methods of classes in the System.Collections.Generic namespace.

  • Simple types all implement interface IComparable<T>.

  • It’s the responsibility of the programmer who declares a type that implements IComparable<T> to provide method CompareTo such that it compares the contents of two objects of that type and returns 0 if they’re equal, a negative value if the first is less than the second and a positive value if the first is greater than the second.

  • You can restrict the types that can be used with a generic method or class to ensure that they meet certain requirements. This feature—known as a type constraint—restricts the type of the argument supplied to a particular type parameter. For example, the clause where T: IComparable<T> indicates that the type arguments must implement interface IComparable<T>. If no type constraint is specified, the default type constraint is object.

  • A class constraint indicates that the type argument must be an object of a specific base class or one of its subclasses.

  • An interface constraint indicates that the type argument’s class must implement a specific interface.

  • You can specify that the type argument must be a reference type or a value type by using the reference-type constraint (class) or the value-type constraint (struct), respectively.

  • You can specify a constructor constraint—new()—to indicate that the generic code can use operator new to create new objects of the type represented by the type parameter. If a type parameter is specified with a constructor constraint, the type argument’s class must provide a public parameterless or default constructor to ensure that objects of the class can be created without passing constructor arguments; otherwise, a compilation error occurs.

  • It’s possible to apply multiple constraints to a type parameter by providing a comma-separated list of constraints in the where clause.

  • If you have a class constraint, reference-type constraint or value-type constraint, it must be listed first—only one of these types of constraints can be used for each type parameter. Interface constraints (if any) are listed next. The constructor constraint is listed last (if there is one).

Section 20.5 Overloading Generic Methods

  • A generic method may be overloaded. All methods must contain a unique signature.

  • A generic method can be overloaded by nongeneric methods with the same method name. When the compiler encounters a method call, it searches for the method declaration that most precisely matches the method name and the argument types specified in the call.

Section 20.6 Generic Classes

  • A generic class provides a means for describing a class in a type-independent manner.

  • Once you have a generic class, you can use a simple, concise notation to indicate the actual type(s) that should be used in place of the class’s type parameter(s). At compilation time, the compiler ensures the type safety of your code, and the runtime system replaces type parameters with actual arguments to enable your client code to interact with the generic class.

  • A generic class declaration is similar to a nongeneric class declaration, except that the class name is followed by a type-parameter list and optional constraints on its type parameters.

  • As with generic methods, the type-parameter list of a generic class can have one or more type parameters separated by commas, and each type parameter can have type constraints.

  • When a generic class is compiled, the compiler performs type checking on the class’s type parameters to ensure that they can be used with the code in the generic class. The constraints determine the operations that can be performed on the variables declared with type parameters.

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

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