- C# without Generics
- Introducing Generic Types
- Constraints
- Generic Methods
- Generic Internals
- Summary
Introducing Generic Types
Generics provide a facility for creating data structures that are specialized to handle specific types when declaring a variable. Programmers define these parameterized types so that each variable of a particular generic type has the same internal algorithm but the types of data and method signatures can vary based on programmer preference.
To minimize the learning curve for developers, C# designers chose syntax that matched the similar templates concept of C++. In C#, therefore, the syntax for generic classes and structures uses the same angle bracket notation to identify the data types on which the generic declaration specializes.
Using a Generic Class
Listing 11.6 shows how you can specify the actual type used by the generic class. You instruct the path variable to use a Cell type by specifying Cell within angle bracket notation in both the instantiation and the declaration expressions. In other words, when declaring a variable (path in this case) using a generic data type, C# requires the developer to identify the actual type. An example showing the new Stack class appears in Listing 11.6.
Example 11.6. Implementing Undo with a Generic Stack Class
using System; using System.Collections.Generic; class Program { // ... public void Sketch() { Stack<Cell> path; // Generic variable declaration path = new Stack<Cell>(); // Generic object instantiation Cell currentPosition; ConsoleKeyInfo key; // New with C# 2.0 do { // Etch in the direction indicated by the // arrow keys entered by the user. key = Move(); switch (key.Key) { case ConsoleKey.Z: // Undo the previous Move. if (path.Count >= 1) { // No cast required. currentPosition = path.Pop(); Console.SetCursorPosition( currentPosition.X, currentPosition.Y); Undo(); } break; case ConsoleKey.DownArrow: case ConsoleKey.UpArrow: case ConsoleKey.LeftArrow: case ConsoleKey.RightArrow: // SaveState() currentPosition = new Cell( Console.CursorLeft, Console.CursorTop); // Only type Cell allowed in call to Push(). path.Push(currentPosition); break; default: Console.Beep(); // New with C#2.0 break; } } while (key.Key != ConsoleKey.X); // Use X to quit. } }
The results of Listing 11.6 appear in Output 11.2.
In the path declaration shown in Listing 11.6, you declare and create a new instance of a System.Collections.Generic.Stack<T> class and specify in angle brackets that the data type used for the path variable is Cell. As a result, every object added to and retrieved from path is of type Cell. In other words, you no longer need to cast the return of path.Pop() or ensure that only Cell type objects are added to path in the Push() method. Before examining the generic advantages, the next section introduces the syntax for generic class definitions.
Example 11.2.
Defining a Simple Generic Class
Generics allow you to author algorithms and patterns, and reuse the code for different data types. Listing 11.7 creates a generic Stack<T> class similar to the System.Collections.Generic.Stack<T> class used in the code in Listing 11.6. You specify a type parameter identifier or type parameter (in this case, T) within angle brackets after the class declaration. Instances of the generic Stack<T> then collect the type corresponding to the variable declaration without converting the collected item to type object. The type parameter T is a placeholder until variable declaration and instantiation, when the compiler requires the code to specify the type parameter. In Listing 11.7, you can see that the type parameter will be used for the internal Items array, the type for the parameter to the Push() method, and the return type for the Pop() method.
Example 11.7. Declaring a Generic Class, Stack<T>
public class Stack<T> { private T[] _Items; public void Push(T data) { ... } public T Pop() { ... } }
Benefits of Generics
There are several advantages to using a generic class (such as the System.Collections.Generic.Stack<T> class used earlier instead of the original System.Collections.Stack type).
- Generics facilitate a strongly typed programming model, preventing data types other than those explicitly intended by the members within the parameterized class. In Listing 11.7, the parameterized stack class restricts you to the Cell data type for all instances of Stack<Cell>. (The statement path.Push("garbage") produces a compile-time error indicating that there is no overloaded method for System.Collections.Generic.Stack<T>.Push(T) that can work with the string garbage, because it cannot be converted to a Cell.)
- Compile-time type checking reduces the likelihood of InvalidCastException type errors at runtime.
- Using value types with generic class members no longer causes a cast to object; they no longer require a boxing operation. (For example, path.Pop() and path.Push() do not require an item to be boxed when added or unboxed when removed.)
- Generics in C# reduce code bloat. Generic types retain the benefits of specific class versions, without the overhead. (For example, it is no longer necessary to define a class such as CellStack.)
- Performance increases because casting from an object is no longer required, thus eliminating a type check operation. Also, performance increases because boxing is no longer necessary for value types.
- Generics reduce memory consumption because the avoidance of boxing no longer consumes memory on the heap.
- Code becomes more readable because of fewer casting checks and because of the need for fewer type-specific implementations.
- Editors that assist coding via some type of IntelliSense work directly with return parameters from generic classes. There is no need to cast the return data for IntelliSense to work.
At their core, generics offer the ability to code pattern implementations and then reuse those implementations wherever the patterns appear. Patterns describe problems that occur repeatedly within code, and templates provide a single implementation for these repeating patterns.
Type Parameter Naming Guidelines
Just as when you name a method parameter, you should be as descriptive as possible when naming a type parameter. Furthermore, to distinguish it as being a type parameter, the name should include a "T" prefix. For example, in defining a class such as EntityCollection<TEntity> you use the type parameter name "TEntity."
The only time you would not use a descriptive type parameter name is when the description would not add any value. For example, using "T" in the Stack<T> class is appropriate since the indication that "T" is a type parameter is sufficiently descriptive; the stack works for any type.
In the next section, you will learn about constraints. It is a good practice to use constraint descriptive type names. For example, if a type parameter must implement IComponent, consider a type name of "TComponent."
Generic Interfaces and Structs
C# 2.0 supports the use of generics in all parts of the C# language, including interfaces and structs. The syntax is identical to that used by classes. To define an interface with a type parameter, place the type parameter in angle brackets, as shown in the example of IPair<T> in Listing 11.8.
Example 11.8. Declaring a Generic Interface
interface IPair<T> { T First { get; set; } T Second { get; set; } }
This interface represents pairs of like objects, such as the coordinates of a point, a person's genetic parents, or nodes of a binary tree. The type contained in the pair is the same for both items.
To implement the interface, you use the same syntax as you would for a nongeneric class. However, implementing a generic interface without identifying the type parameter forces the class to be a generic class, as shown in Listing 11.9. In addition, this example uses a struct rather than a class, indicating that C# supports custom generic value types.
Example 11.9. Implementing a Generic Interface
public struct Pair<T>: IPair<T> { public T First { get { return _First; } set { _First = value; } } private T _First; public T Second { get { return _Second; } set { _Second = value; } } private T _Second; }
Support for generic interfaces is especially important for collection classes, where generics are most prevalent. Without generics, developers relied on a series of interfaces within the System.Collections namespace. Like their implementing classes, these interfaces worked only with type object, and as a result, the interface forced all access to and from these collection classes to require a cast. By using generic interfaces, you can avoid cast operations, because a stronger compile-time binding can be achieved with parameterized interfaces.
Defining a Constructor and a Finalizer
Perhaps surprisingly, the constructor and destructor on a generic do not require type parameters in order to match the class declaration (i.e., not Pair<T>(){...}). In the pair example in Listing 11.11, the constructor is declared using public Pair(T first, T second).
Example 11.11. Declaring a Generic Type's Constructor
public struct Pair<T>: IPair<T> { public Pair(T first, T second) { _First = first; _Second = second; } public T First { get{ return _First; } set{ _First = value; } } private T _First; public T Second { get{ return _Second; } set{ _Second = value; } } private T _Second; }
Specifying a Default Value
Listing 11.1 included a constructor that takes the initial values for both First and Second, and assigns them to _First and _Second. Since Pair<T> is a struct, any constructor you provide must initialize all fields. This presents a problem, however. Consider a constructor for Pair<T> that initializes only half of the pair at instantiation time.
Defining such a constructor, as shown in Listing 11.12, causes a compile error because the field _Second goes uninitialized at the end of the constructor. Providing initialization for _Second presents a problem since you don't know the data type of T. If it is a reference type, then null would work, but this would not suffice if T were a value type (unless it was nullable).
Example 11.12. Not Initializing All Fields, Causing a Compile Error
public struct Pair<T>: IPair<T> { // ERROR: Field 'Pair<T>._second' must be fully assigned // before control leaves the constructor // public Pair(T first) // { // _First = first; // } // ... }
To deal with this scenario, C# 2.0 allows a dynamic way to code the default value of any data type using the default operator, first discussed in Chapter 8. In Chapter 8, I showed how the default value of int could be specified with default(int) while the default value of a string uses default(string) (which returns null, as it would for all reference types). In the case of T, which _Second requires, you use default(T) (see Listing 11.13).
Example 11.13. Initializing a Field with the default Operator
public struct Pair<T>: IPair<T> { public Pair(T first) { _First = first; _Second = default(T); } // ... }
The default operator is allowable outside of the context of generics; any statement can use it.
Multiple Type Parameters
Generic types may employ any number of type parameters. The initial Pair<T> example contains only one type parameter. To enable support for storing a dichotomous pair of objects, such as a name/value pair, you need to extend Pair<T> to support two type parameters, as shown in Listing 11.14.
Example 11.14. Declaring a Generic with Multiple Type Parameters
interface IPair<TFirst, TSecond> { TFirst First { get; set; } TSecond Second { get; set; } } public struct Pair<TFirst, TSecond>: IPair<TFirst, TSecond> { public Pair(TFirst first, TSecond second) { _First = first; _Second = second; } public TFirst First { get{ return _First; } set{ _First = value; } } private TFirst _First; public TSecond Second { get{ return _Second; } set{ _Second = value; } } private TSecond _Second; }
When you use the Pair<TFirst, TSecond> class, you supply multiple type parameters within the angle brackets of the declaration and instantiation statements, and then you supply matching types to the parameters of the methods when you call them, as shown in Listing 11.15.
Example 11.15. Using a Type with Multiple Type Parameters
Pair<int, string> historicalEvent = new Pair<int, string>(1914, "Shackleton leaves for South Pole on ship Endurance"); Console.WriteLine("{0}: {1}", historicalEvent.First, historicalEvent.Second);
The number of type parameters, the arity, uniquely distinguishes the class. Therefore, it is possible to define both Pair<T> and Pair<TFirst, TSecond> within the same namespace because of the arity variation.
Nested Generic Types
Nested types will automatically inherit the type parameters of the containing type. If the containing type includes a type parameter T, for example, then the type T will be available on the nested type as well. If the nested type includes its own type parameter named T, then this will hide the type parameter within the containing type and any reference to T in the nested type will refer to the nested T type parameter. Fortunately, reuse of the same type parameter name within the nested type will cause a compiler warning to prevent accidental overlap (see Listing 11.16).
Example 11.16. Nested Generic Types
class Container<T, U> { // Nested classes inherit type parameters. // Reusing a type parameter name will cause // a warning. class Nested<U> { void Method(T param0, U param1) { } } }
The behavior of making the container's type parameter available in the nested type is consistent with nested type behavior in the sense that private members of the containing type are also accessible from the nested type. The rule is simply that a type is available anywhere within the curly braces within which it appears.
Type Compatibility between Generic Classes with Type-Compatible Type Parameters
If you declare two variables with different type parameters using the same generic class, the variables are not type compatible; they are not covariant. The type parameter differentiates two variables of the same generic class but with different type parameters. For example, instances of a generic class, Stack<Contact> and Stack<PdaItem>, are not type compatible even when the type parameters are compatible. In other words, there is no built-in support for casting Stack<Contact> to Stack<PdaItem>, even though Contact derives from PdaItem (see Listing 11.17).
Example 11.17. Conversion between Generics with Different Type Parameters
using System.Collections.Generic; ... // Error: Cannot convert type ... Stack<PdaItem> exceptions = new Stack<Contact>();
To allow this you would have to subtly cast each instance of the type parameter, possibly an entire array or collection, which would hide a potentially significant performance cost.