#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

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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

  1. Pingback: #544 – Specifying a Constraint for a Type Parameter « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: