How do I use Generics with interfaces?

G

Guest

I want to have an object with a method that accepts a List of interfaces, for
example:
List<IMyInterface> myInterfaces

Then in another object I have a List of objects which implement that
interface, like:

MyObject : IMyInterface
so the list is List<MyObject>

I cannot figure out how to pass my list of objects to that method that
accepts a list of the interfaces. It keeps saying cannot convert the values...
 
T

Tom Spink

MrNobody said:
I want to have an object with a method that accepts a List of interfaces,
for example:
List<IMyInterface> myInterfaces

Then in another object I have a List of objects which implement that
interface, like:

MyObject : IMyInterface
so the list is List<MyObject>

I cannot figure out how to pass my list of objects to that method that
accepts a list of the interfaces. It keeps saying cannot convert the
values...

Hi,

Which method are you talking about? And what exactly are you trying to do?
Add the contents of a List<MyObject> to a List<IMyInterface>?
 
M

Marc Gravell

void SomeMethod<T>(List<T> data) where T : IMyInterface {}

(you could possibly substutute List<T> for IList<T> or
IEnumberable<T>)

The point is that List<MyObject> *does not* inherit/implement
List<IMyInterface> just becuase MyObject : IMyInterface.

Hope this helps,

Marc
 
P

PS

MrNobody said:
I want to have an object with a method that accepts a List of interfaces,
for
example:
List<IMyInterface> myInterfaces

class MyClass<T> where T : IMyInterface
{
Then in another object I have a List of objects which implement that
interface, like:

This question is unclear. You are already constraining the object to a type
like

MyClass<MyListItem> objectWithList = new MyClass<MyListItem>();
objectWithList.MyMethod(new List<MyListItem>());

Do you want a second interface for the class that contains a method that
accepts a List of items of type IMyInterface?

Then you would need this

class MyClass<T> : IMyClass<T> where T : IMyInterface
{
public void MyMethod(List<T> myList){....}
}

interface IMyClass<T> where T : IMyInterface
{
void MyMethod(List<T> myList);
}

I just did an extract interface refactoring on the first code I posted.

MyObject : IMyInterface
so the list is List<MyObject>

I cannot figure out how to pass my list of objects to that method that
accepts a list of the interfaces. It keeps saying cannot convert the
values...

PS
 
T

Tom Spink

Tom said:
Hi,

Which method are you talking about? And what exactly are you trying to
do? Add the contents of a List<MyObject> to a List<IMyInterface>?

Hi,

I think I see what you're trying to do, and as Marc has mentioned, it's a
bit of a tricky situation, as C# doesn't support generic covariance.

I see what you're trying to do now, pass a List<MyObject> to
List<IMyInterface>, but as far as C# is concerned, these two types are
distinct and not related. Like I said, C# doesn't support generic
covariance.

There are a number of ways you could approach this problem, perhaps by using
arrays instead of lists, as array covariance *is* supported. If you use
arrays, but start off with lists you can use the ToArray method of the List
object to get an array representation of the contents of the list. But
remember, you won't be able to modify those lists you're passing in - so
this approach is fine if you just want to read the array and not
concatenate/delete from it.
 
G

Guest

Sorry I was not explaining my situation very well, but yeah Tom that's the
problem I am facing

I have this object which implements an interface and I want a method whose
signature is List<IMyInterface> to basically accept a List<MyObject> through
polymorphism. But I guess tht ain't happening.

I will see if I can use arrays instead.

I am very green on this Generics topic, but if I try making a class with a
"T" somewhere in it my IDE complains. I imported System.Common.Generics, is
there some other namespace I need to use to get those T's recognized?
 
A

AlexS

Is that what you are talking about?

class MyObject : IMyInterface { ... }
....
List<IMyInterface> list = new List<IMyInterface>();
list.Add(new MyObject());
....

HTH
Alex
 
G

Guest

AlexS said:
Is that what you are talking about?

close,

like this:

class MyObject : IMyInterface { ... }
....
public void doSomething(List<IMyInterface> list) { ... }
.....
List<MyObject> list = new List<MyObject>();
doSomething(list);
 
T

Tom Spink

MrNobody said:
close,

like this:

class MyObject : IMyInterface { ... }
....
public void doSomething(List<IMyInterface> list) { ... }
....
List<MyObject> list = new List<MyObject>();
doSomething(list);

Hi,

So we've already discussed that this isn't possible, due to the lack of
support for generic covariance. But array covariance *is* supported, have
you had any luck trying this?
 
G

Guest

Yes, I'm sorry I forgot to mention I changed it to use Arrays and it works
fine. It is not a problem because at the point I am going to be passing it to
other functions I am not going to add or remove contents from the list so
Array is fine.
 

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