List<T<U>> in c# where T and U are interfaces

.net, .net-3.5, c#, generics

Solution

I think you should use covariance (.NET 4.0)

public interface IPlate<out T> where T : IWaffle {}

and replace `IPancake` with `IWaffle`

var plates = new List<IPlate<IPancake>>();

Problem

Okay. So I am currently trying to create a list of an interface in C# that takes an interface as a parameter. To make this clearer, let me give an example: ``` public interface IPlate<T> where T : IWaffle {} public interface IWaffles {} public class BelgiumWaffle : IWaffle {} public class FalafelWaffle : IWaffle {} public class HugePlate : IPlate<BelgiumWaffle> {} public class SmallPlate : IPlate<FalafelWaffle> {} // Now, I want to do the following: var plates = new List<IPlate<IWaffle>>(); plates.add(new HugePlate()); plates.add(new SmallPlate()); ``` The goal is to be able to serialize a list of IPlate objects into XML. I was hoping to use generics to do this but I keep getting errors telling me that there are some argument errors when trying to add (aka - the types don't match up). Just not sure what I'm doing wrong here. It seems right to me but I must be missing something (obviously). Update: I should mention that this is .NET v3.5 Update: Sorry! Some typos when writing the question regarding definition of Plate classes.

Original source

Related problems