views:

126

answers:

3

If have a set of classes that all implement an interface.

interface IMyinterface<T>
{
    int foo(T Bar);
}

I want to shove them all in a list and enumerate through them.

 List<IMyinterface> list
 foreach(IMyinterface in list)
 // etc...

but the compiler wants to know what T is. Can I do this? How can I overcome this issue?

+7  A: 

There is no IMyinterface type there is only a IMyinterface`1 type which will require a type argument. You could create an IMyinterface type:-

interface IMyinterface { ... }

then inherit from it

interface IMyinterface<T> : IMyinterface { ... }

You would need to move any members you would like to use in the foreach loop to the IMyinterface definition.

AnthonyWJones
+1  A: 

If you plan to invoke a method with T in the signature, the answer is that you cannot. Otherwise you can do as anthonywjones suggests

krosenvold
+1  A: 

You still need to tell the compiler what T is at some time, but just what you asked can be done:

 interface IMyinterface
{
    int foo<T>(T Bar);
}

List<IMyinterface> list = new List<IMyinterface>();
foreach(IMyinterface a in list){}
KeesDijk