Tom Spink wrote:
Quote:
MrNobody wrote:
>
Quote:
>I want to have an object with a method that accepts a List of interfaces,
>for example:
>List<IMyInterfacemyInterfaces
>>
>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<MyObjectto 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<MyObjectto
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.
--
Tom Spink
University of Edinburgh