Inheritance

Generic types can be inherited, but some basic rules apply. For example, the derived class cannot be a closed constructed type. Table 7-1 lists all the possible permutations.

Table 7-1. Inheritance table for generic types

Base class

Derived class

Comments

Generic (open)

Generic (open)

Permitted when the derived class uses the type parameters of the base class

Generic (open)

Generic (closed)

Not permitted

Generic (open)

Non-generic

Permitted

Generic (closed)

Generic (open)

Permitted

Generic (closed)

Generic (closed)

Not permitted

Generic (closed)

Non-generic

Not permitted

Non-generic

Generic (closed)

Permitted

Non-generic

Generic (open)

Not permitted

This sample code shows some of the combinations that are permitted and not permitted:

public class ZClass<T> { } ...

Get Programming Microsoft® Visual C#® 2008: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.