I'm trying to use polymorphism in a constructor but cannot make it work without workaround.
I have :
public class A { }
public class B : A { }
Why doesn't this work :
IList<B> blist = new List<B> ...
IList<A> alist = (IList<A>)blist ;
When the same without the list works fine:
B bt = new B..
A a = (A)b;
This is especially annoying when wanting to use a list in a constructor, especially with the c# limitation of calling the base constructor before doing anything else. which forbid to do this :
public X(IList<B> param) : base((IList<A> param))
{}
Any way to do it properly without calling a dummy base() and rewriting the constructor completely ?
One way I found is doing : base( sections.Select(b => (A) b).ToList() )
but It feel quite klunky...
Edit:
This question emphasis on the construction aspect.
For this case I use a List because the order is important and the amount of elements could vary. answers on this post and similar post focus on using Array or Enumerable. However Array is limited when resizing and Enumerable does not guarantee the order.