Control Generic Type Parameter

J

Jinsong Liu

Hello Group:

I am playing with .NET 2.0 Generic. I have a CarsList<T> Generic
class, Is it possible that I can control what T could be? I want to
ensure the T can only be classes derived from a specific class (lets
say Car).
I understand that I can create a CarList without using Generic, but
when I add a derived class to the list, type conversion will happen,
and I will not be able to enforce type at child class level.

thanks

JL
 
J

Jinsong Liu

I just found the answer, the "where" is what I need. I can use where
keyword to specify constraints on the types that can be used as
arguments for a type parameter defined in a generic declaration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top