Reputation: 93
Let's say I have these classes:
public abstract class Thing<T> { }
public class Str : Thing<string> { }
public class Boo : Thing<bool> { }
I'd like to create a List
of Thing
.
This is an example of what I'd like to do (invalid C# code):
new List<Something>()
{
new Str(),
new Boo()
};
How could I do something like that, would it even be possible?
I found this but couldn't seem to correctly understand any of the proposed answers.
Upvotes: 0
Views: 334
Reputation: 1264
To satisfy List<Something>
then Thing<T>
must inherit Something
. The answers in the post you linked have the solution: you need to introduce a non-generic base class.
public abstract class Something { }
public abstract class Thing<T>: Something { }
public class Str : Thing<string> { }
public class Boo : Thing<bool> { }
new List<Something>()
{
new Str(),
new Boo()
};
Upvotes: 1