Generic Type

A GenericType is one that is parameterized by a type. E.g. in Eiffel you can declare a variable thus:

x : LIST[INTEGER] y : LIST[ARRAY[INTEGER]]

Often collection types are generic (on their element type).

E.g. the array type is generic in Java, or Pascal or even C. Thus one can declare, in Java:

int[] myArray;

and the Java compiler will guarantee that all the elements of myArray are int's.

What you can't write is:

Vector of int myVector;

and have the compiler guarantee that all the elements of mtVector are int's.

- StephenHutchinson


I just read a paper about how the .NET team wants to add generics to the CLR. Specifically, C# is going to support generics with a C++ template-like syntax. The inner workings appear to be very different, though. While C++ templates are (more or less) code expansion at compile time the CLR supports Generics at instantiation time. As I read on I realized that C# Generics really are generic: if you instantiate a parametrized type with a reference type (class, as opposed to something like int or float) you can only use System.Object members inside your parametrized methods. That's why they added 'constraints' to the language. By adding constraints (kind of like specifying an interface) you can tell the compiler which interfaces the parametrizer has to support.

So, it sounds like C++ templates allow us to write code that will work with any type that supports certain functions (name-polymorphism?) while C# do the exact opposite. C# Generics allow us to write parametrizable code that will work only with specific types, moving away from the uglyness of downcasting everything from System.Object.

--AndrewQueisser


EditText of this page (last edited August 29, 2003) or FindPage with title or text search