How to do generic polymorphism on open types in C#?

Since you don’t want to force a specific type, make an abstract class or an interface this way:

interface IFoo { }

And inherit it in your type-specific Foo (with generic parameters), like:

interface IFoo<T> : IFoo {}

This way you can just have a List<IFoo> and add IFoo<T> instead.

More examples:

class FooCollection {
    private List<IFoo> _collection;

    public FooCollection()
    {
        _collection = new List<IFoo>();
    }

    public void Add(IFoo foo)
    {
        _collection.Add(foo);
    }

    public void Remove(IFoo foo)
    {
        _collection.Remove(foo);
    }
}

Leave a Comment