I have an interface:
interface IFoo<out T>
{
T Get();
}
and some instances like IFoo<int> a, IFoo<User> u, IFoo<string> s and etc. There is a List<IFoo<object>> used to collect them. But variance doesn’t work for value types, is there a proper way to put them in the list?
It doesn’t look like you need generics for this list, so you can have the interface implement a non-generic interface:
That way, all of your objects implement the same interface. This may not be a bad idea, since they do have something in common. Now you can simply use a
List<IFoo>.