where t : multiple classes

yoni picture yoni · Jun 29, 2010 · Viewed 12.1k times · Source
List<T> Foo<T>(Ilist list) 
where T : ??

is there any way to enforce T to be
one of few classes ?

eventually i want to do a switch on T..

thanks.

Answer

Mark Byers picture Mark Byers · Jun 29, 2010

You can require that each class you want to allow into your list implements some interface ISomething and then create a List<ISomething>.

eventually i want to do a switch on T..

Instead of a switch on the type of the object it might be better to have a method in your interface and implement it differently for each class.