I know it's not possible to cast derived lists (why not is nicely explained here), but what about ImmutableArrays as they cannot be changed casting should be valid, right?
I would expect this to work:
class Animal {}
class Dog : Animal {}
ImmutableArray<Dog> dogs = ImmutableArray.Create<Dog>(new Dog());
ImmutableArray<Animal> animals = (ImmutableArray<Animal>) dogs;
ImmutableArray<Dog> dogsBack = (ImmutableArray<Dog>) animals;
Yes this is possible:
Note that you could also use
CastArrayto cast from dogs to animals, but due to covariance,CastUpis a significantly faster than theCastArraymethod.