A generic class that uses parameterized types, like MyBase<T>, is called an open-constructed generic. A generic class that uses no parameterized types, like MyBase<int>, is called a closed-constructed generic.

You may derive from a closed-constructed generic; that is, you may inherit a class named MyDerived from another class named MyBase, as in:


public class MyDerived<T> : MyBase1<int>

You may derive from an open-constructed generic, provided the type is parameterized.

For example:

public class MyDerived<T> : MyBase<T> is valid, but


public class MyDerived<T> : MyBase<Y> is not valid since Y is also a parameterized type.

Note: Non-generic classes may derive from closed-constructed generic classes, but not from open-constructed generic classes. That is,

public class MyDerived : MyBase<int> is valid, but


public class MyDerived : MyBase<T> is not valid.

-------

A generic class allows you to write your class without committing to any type, yet allows the user of your class, later on, to indicate the specific type to be used. While this gives greater flexibility by placing some constraints on the types that may be used for the parameterized type, you gain some control in writing your class. Let's look at an example:

Example 1. The need for constraints: code that will not compile


public static T Max<T>(T op1, T op2)
{
        if (op1.CompareTo(op2) < 0)
                return op1;
        return op2;
}


The code in Example 1 will produce a compilation error:

Error 1 'T' does not contain a definition for 'CompareTo'

Assume I need the type to support the CompareTo() method. I can specify this by using the constraint that the type specified for the parameterized type must implement the IComparable interface. Example 2 has the code:

Example 2. Specifying a constraint


public static T Max<T>(T op1, T op2) where T : IComparable
{
        if (op1.CompareTo(op2) < 0)
                return op1;
        return op2;
}

In Example 2, I have specified the constraint that the type used for parameterized type must inherit from (implement) IComparable. The following are some of the other constraints may be used:


where T : struct                   type must be a value type (a struct)
where T : class                   type must be reference type (a class)
where T : new()                  type must have a no-parameter constructor
where T : class_name      type may be either class_name or one of its
                                              sub-classes (or is below class_name
                                              in the inheritance hierarchy)

where T : interface_name  type must implement the specified interface
You may specify a combination of constraints, as in: where T : IComparable, new(). This says that the type for the parameterized type must implement the IComparable interface and must have a no-parameter constructor.

------