How do I create a list of objects that inherit from the same generic class with varying types?

So, if I understand correctly, you want something like this: List : GenericType { } public class AType : GenericType { } public class BType : GenericType { } You can then stick any instance into a List.

So, if I understand correctly, you want something like this: List Which can contain any instance of AType, BType, etc. You simply do this by making GenericType itself a subclass of GenericType: public class GenericType { } public class GenericType : GenericType { } public class AType : GenericType { } public class BType : GenericType { } You can then stick any instance into a List.

Can't believe I didn't think of that - thanks very, very much! – yaztheblack Aug 15 at 16:49.

That's not possible in the C# type system; you can only make a list of an actual type, not of a "type pattern" like "List where U must be Foo for some unspecified T". To see why it is impossible, let's hypothetically suppose that it was possible. We'll annotate the "type pattern" as a question mark: class Stack { ... } ... List> listOfStacks = new List>(); listOfStacks.

Add(new Stack()); listOfStacks. Add(new Stack()); Ok, great. Now what are you going to do with the list of stacks?

You can't safely do anything with the stack that involves the type parameter of the stack: listOfStacks0. Push("hello"); That should fail at compile time because the stack in slot zero is a stack of ints, not strings. How can the compiler know that?

The compiler cannot know that. By allowing this feature, you essentially break the ability of the compiler to type-check your program.To answer "how do I do this thing which is impossible? " it is helpful to know why you want to do the impossible thing.

Instead of trying to do this impossible thing tell us what you are really trying to do and see if anyone can solve the real problem.

I agree with the gist of your answer, but it's not true that "you can't safely do anything with the stack". For instance, using such a feature you could safely access the Count property, or call the Clear method. – kvb Aug 16 at 15:45 @kvb: good point.

– Eric Lippert Aug 16 at 16:29 @Tejs gave an answer that seems to work. To clarify, each implementation overrides a method that takes in an object of type T and returns a bool - the List is for looping over to run that method. – yaztheblack Aug 17 at 8:12.

If GenericType implemented a non-generic interface IAmNotGeneric, then both ATypes and BTypes could be added to a List. Public abstract class GenericType : IAmNotGeneric The same principle could be applied with a non-generic abstract base class. Public abstract class GenericType : NonGenericType.

Assembly. GetExecutingAssembly(). GetTypes .

Where(type => type.BaseType. IsGenericType && type.BaseType. GetGenericTypeDefinition() == typeof(GenericType)) .ToList().

. ForEach(type => list. Add(Activator.

CreateInstance(type))).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions