Given an interface IQuestion
and an implementation of that interface AMQuestion
, suppose the following example:
List<AMQuestion> typed = new List<AMQuestion>(); IList<IQuestion> nonTyped = typed;
This example yields, as expected, a compile error saying the two are not of the same type. But it states an explicit conversion exists. So I change it to look like this:
List<AMQuestion> typed = new List<AMQuestion>(); IList<IQuestion> nonTyped = typed as IList<IQuestion>;
Which then compiles but, at run time, nonTyped
is always null. If someone could explain two things:
It would be greatly appreciated. Thank you!
The fact that AMQuestion
implements the IQuestion
interface does not translate into List<AMQuestion>
deriving from List<IQuestion>
.
Because this cast is illegal, your as
operator returns null
.
You must cast each item individually as such:
IList<IQuestion> nonTyped = typed.Cast<IQuestion>().ToList();
Regarding your comment, consider the following code, with the usual cliché animal examples:
//Lizard and Donkey inherit from Animal List<Lizard> lizards = new List<Lizard> { new Lizard() }; List<Donkey> donkeys = new List<Donkey> { new Donkey() }; List<Animal> animals = lizards as List<Animal>; //let's pretend this doesn't return null animals.Add(new Donkey()); //Reality unravels!
if we were allowed to cast List<Lizard>
to a List<Animal>
, then we could theoretically add a new Donkey
to that list, which would break inheritance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With