A List<Child>
cannot be assigned to a List<Parent>
variable. They are unrelated types. Here's a "proof" by contradiction.
Suppose List<Child>
can be assigned to List<Parent>
, then this would be allowed by the compiler:
List<Parent> parents = new List<Child>();
parents.Add(new Child2()); // Child2 is another subclass of "Parent"
However, this creates a contradiction. parents
actually stores a List<Child>
, which is not capable of storing Child2
objects.
A workaround you can use here is to create a List<Parent>
object, and add your Child
objects into that list:
var.Add("string", new List<Parent>());
List<Child> children = new List<Child> { ... }
var["string"].AddRange(children);