Home > OS >  Making arrays to hold polymorphed generics
Making arrays to hold polymorphed generics

Time:10-04

The question I asked here brought me thus far on my project:

abstract class Base<T> where T:ContentBase
{
    //base functionality
}

class Foo : Base<FooContent>
{
    //overrides
}

class Bar : Base<BarContent>
{
    //overrides
}

Is there a way to make an array or List that can hold both Foo and Bar objects or would I have to look at making a record or something?

Thanks in advance.

CodePudding user response:

I would suggest creating an Interface. Add the interface to the base class and define the list or array in terms of the interface.

With an interface anything that implements the interface can be in a generic collection of that interface.

CodePudding user response:

enter image description here Cred's to this person who commented here, I don't know how to credit a comment as an answer but this was exactly what I was looking for! :)

  • Related