12

Generics

As your projects become more sophisticated, you will need a better way to reuse and customize existing software. To facilitate code reuse, especially the reuse of algorithms, C# includes a feature called generics.

Generics are lexically like generic types in Java and templates in C++. In all three languages, these features enable the implementation of algorithms and patterns once, rather than requiring separate implementations for each type that the algorithm or pattern operates on. However, C# generics are very different from both Java generics and C++ templates in the details of their implementation and impact on the type system of their respective languages. Just as methods are powerful because they can take arguments, so types and methods that take type arguments have significantly more functionality.

Generics were added to the runtime and C# in version 2.0.

C# without Generics

We begin the discussion of generics by examining a class that does not use generics. This class, System.Collections.Stack, represents a collection of objects such that the last item to be added to the collection is the first item retrieved from the collection (last in, first out [LIFO]). Push() and Pop(), the two main methods of the Stack class, add items to the stack and remove them from the stack, respectively. The declarations for the methods on the Stack class appear in Listing 12.1.

Listing 12.1: The System.Collections.Stack Method Signatures
public class Stack
{
    public virtual object Pop()
    {
        // ...
    }
 
    public virtual void Push(object obj)
    {
        // ...
    }
    // ...
}

Programs frequently use stack type collections to facilitate multiple undo operations. For example, Listing 12.2, with results shown in Output 12.1, uses the System.Collections.Stack class for undo operations within a program that simulates an Etch A Sketch toy.

Listing 12.2: Supporting Undo in a Program Similar to an Etch A Sketch Toy
using System.Collections.Generic;
 
public class Program
{
    // ...
 
    public static void Sketch()
    {
        Stack<Cell> path = new();
        Cell currentPosition;
        ConsoleKeyInfo key;
        // ...
 
        do
        {
            // Etch in the direction indicated by the
            // arrow keys that the user enters
            key = Move();
 
            switch(key.Key)
            {
                case ConsoleKey.Z:
                    // Undo the previous Move
                    if(path.Count >= 1)
                    {
                        currentPosition = (Cell)path.Pop();
                        Console.SetCursorPosition(
                            currentPosition.X, currentPosition.Y);
                        // ...
                        Undo();
                    }
                    break;
                case ConsoleKey.DownArrow:
                    // ...
                case ConsoleKey.UpArrow:
                    // ...
                case ConsoleKey.LeftArrow:
                    // ...
                case ConsoleKey.RightArrow:
                    // SaveState()
                    if(Console.CursorLeft < Console.WindowWidth - 2)
                    {
                        currentPosition = new Cell(
                            Console.CursorLeft + 1, Console.CursorTop);
                    }
                    path.Push(currentPosition);
                    // ...
                    break;
 
                default:
                    Console.Beep();
                    break;
            }
        }
        while(key.Key != ConsoleKey.X);  // Use X to quit.
    }
    // ...
}
 
public struct Cell
{
    public int X { get; }
    public int Y { get; }
 
    public Cell(int x, int y)
    {
        X = x;
        Y = y;
    }
}
Output 12.1

Using the variable path, which is declared as a System.Collections.Stack, you save the previous move by passing a custom type, Cell, into the Stack.Push() method using path.Push(currentPosition). If the user enters a Z (or presses Ctrl+Z), you undo the previous move by retrieving it from the stack using a Pop() method, setting the cursor position to be the previous position, and calling Undo().

Although this code is functional, the System.Collections.Stack class has a fundamental shortcoming. As shown in Listing 12.1, the Stack class collects values of type object. Because every object in the Common Language Runtime (CLR) derives from object, Stack provides no validation that the elements you place into it are homogenous or are of the intended type. For example, instead of passing currentPosition, you can pass a string in which X and Y are concatenated with a decimal point between them. However, the compiler must allow the inconsistent data types because the stack class is written to take any object, regardless of its more specific type.

Furthermore, when retrieving the data from the stack using the Pop() method, you must cast the return value to a Cell. But if the type of the value returned from the Pop() method is not Cell, an exception is thrown. By deferring type checking until runtime by using a cast, you make the program more brittle. The fundamental problem with creating classes that can work with multiple data types without generics is that they must work with a common base class (or interface), usually object.

Using value types, such as a struct or an integer, with classes that use object exacerbates the problem. If you pass a value type to the Stack.Push() method, for example, the runtime automatically boxes it. Similarly, when you retrieve a value type, you need to explicitly unbox the data and cast the object reference you obtain from the Pop() method into a value type. Casting a reference type to a base class or interface has a negligible performance impact, but the box operation for a value type introduces more overhead, because it must allocate memory, copy the value, and then later garbage-collect that memory.

C# is a language that encourages type safety: The language is designed so that many type errors, such as assigning an integer to a variable of type string, can be caught at compile time. The fundamental problem is that the Stack class is not as type-safe as one expects a C# program to be. To change the Stack class to enforce type safety by restricting the contents of the stack to be a particular data type (without using generic types), you must create a specialized stack class, as in Listing 12.3.

Listing 12.3: Defining a Specialized Stack Class
public class CellStack
{
    public virtual Cell Pop() { return new Cell(); } // would return
                                                     // that last cell added and remove it from the list
    public virtual void Push(Cell cell) { }
    // ...
}

Because CellStack can store only objects of type Cell, this solution requires a custom implementation of the stack methods, which is less than ideal. Implementing a type-safe stack of integers would require yet another custom implementation; each implementation would look remarkably like every other one. There would be lots of duplicated, redundant code.

Beginner Topic
Another Example: Nullable Value Types

Chapter 3 introduced the capability of declaring variables that could contain null by using the nullable modifier, ?, when declaring a value type variable. C# began supporting this functionality only in version 2.0, because the right implementation required generics. Prior to the introduction of generics, programmers faced essentially two options.

The first option was to declare a nullable data type for each value type that needs to handle null values, as shown in Listing 12.4.

Listing 12.4: Declaring Versions of Various Value Types That Store null
struct NullableInt
{
    /// <summary>
    /// Provides the value when HasValue returns true
    /// </summary>
    public int Value { getprivate set; }
 
    /// <summary>
    /// Indicates whether there is a value or whether
    /// the value is "null"
    /// </summary>
    public bool HasValue { getprivate set; }
 
    // ...
}
 
struct NullableGuid
{
    /// <summary>
    /// Provides the value when HasValue returns true
    /// </summary>
    public Guid Value { getprivate set; }
 
    /// <summary>
    /// Indicates whether there is a value or whether
    /// the value is "null"
    /// </summary>
    public bool HasValue { getprivate set; }
 
    // ...
}
 
// ...

Listing 12.4 shows possible implementations of NullableInt and NullableGuid. If a program required additional nullable value types, you would have to create yet another struct with the properties modified to use the desired value type. Any improvement of the implementation (e.g., adding a user-defined implicit conversion from the underlying type to the nullable type) would require modifying all the nullable type declarations.

An alternative strategy for implementing a nullable type without generics is to declare a single type with a Value property of type object, as shown in Listing 12.5.

Listing 12.5: Declaring a Nullable Type That Contains a Value Property of Type object
struct Nullable
{
    /// <summary>
    /// Provides the value when HasValue returns true.
    /// </summary>
    public object Value { getprivate set; }
 
    /// <summary>
    /// Indicates whether there is a value or whether
    /// the value is "null"
    /// </summary>
    public bool HasValue { getprivate set; }
 
    // ...
}

Although this option requires only one implementation of a nullable type, the runtime always boxes value types when setting the Value property. Furthermore, retrieving the underlying value from the Value property requires a cast operation, which might potentially be invalid at runtime.

Neither option is particularly attractive. To eliminate this problem, generics1 were added to C#. (And, in fact, nullable types are implemented as the generic type Nullable<T>.)

________________________________________

1. Introduced in C# 2.0.
{{ snackbarMessage }}
;