#543 – Operations You Can Perform on a Generically Typed Object

When you implement a generic class, it accepts one or more type parameters.  The idea of generics is that code in your class can act upon objects whose type matches the type of these parameters.  But their type is only fully determined when the generic class is constructed, that is–when you create an instance of the class and supply concrete types for the type parameters.

This means that the code within your class doesn’t know the actual type of your type parameters. So how can you write code that uses objects of this type?

At compile time, the compiler will assume that objects whose type is one of the type parameters are of type object.  This means that you’re limited to methods defined in System.Object.

In the example below, the list of methods displayed by Intellisense is limited to those appropriate for the object type.

Advertisements