To add to Sweko's point:
The reason why the cast
var listOfX = new List<X>();
ListOf<Y> ys = (List<Y>)listOfX; // Compile error: Cannot implicitly cast X to Y
is not possible is because the List<T>
is invariant in the Type T and thus it doesn't matter whether X
derives from Y
) - this is because List<T>
is defined as:
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T> ... // Other interfaces
(Note that in this declaration, type T
here has no additional variance modifiers)
However, if mutable collections are not required in your design, an upcast on many of the immutable collections, is possible, e.g. provided that Giraffe
derives from Animal
:
IEnumerable<Animal> animals = giraffes;
This is because IEnumerable<T>
supports covariance in T
- this makes sense given that IEnumerable
implies that the collection cannot be changed, since it has no support for methods to Add or Remove elements from the collection. Note the out
keyword in the declaration of IEnumerable<T>
:
public interface IEnumerable<out T> : IEnumerable
(Here's further explanation for the reason why mutable collections like List
cannot support covariance
, whereas immutable iterators and collections can.)
Casting with .Cast<T>()
As others have mentioned, .Cast<T>()
can be applied to a collection to project a new collection of elements casted to T, however doing so will throw an InvalidCastException
if the cast on one or more elements is not possible (which would be the same behaviour as doing the explicit cast in the OP's foreach
loop).
Filtering and Casting with OfType<T>()
If the input list contains elements of different, incompatable types, the potential InvalidCastException
can be avoided by using .OfType<T>()
instead of .Cast<T>()
. (.OfType<>()
checks to see whether an element can be converted to the target type, before attempting the conversion, and filters out incompatable types.)
foreach
Also note that if the OP had written this instead: (note the explicit Y y
in the foreach
)
List<Y> ListOfY = new List<Y>();
foreach(Y y in ListOfX)
{
ListOfY.Add(y);
}
that the casting will also be attempted. However, if no cast is possible, an InvalidCastException
will result.
Examples
For example, given the simple (C#6) class hierarchy:
public abstract class Animal
{
public string Name { get; }
protected Animal(string name) { Name = name; }
}
public class Elephant : Animal
{
public Elephant(string name) : base(name){}
}
public class Zebra : Animal
{
public Zebra(string name) : base(name) { }
}
When working with a collection of mixed types:
var mixedAnimals = new Animal[]
{
new Zebra("Zed"),
new Elephant("Ellie")
};
foreach(Animal animal in mixedAnimals)
{
// Fails for Zed - `InvalidCastException - cannot cast from Zebra to Elephant`
castedAnimals.Add((Elephant)animal);
}
var castedAnimals = mixedAnimals.Cast<Elephant>()
// Also fails for Zed with `InvalidCastException
.ToList();
Whereas:
var castedAnimals = mixedAnimals.OfType<Elephant>()
.ToList();
// Ellie
filters out only the Elephants - i.e. Zebras are eliminated.
Re: Implicit cast operators
Without dynamic, user defined conversion operators are only used at compile-time*, so even if a conversion operator between say Zebra and Elephant was made available, the above run time behaviour of the approaches to conversion wouldn't change.
If we add a conversion operator to convert a Zebra to an Elephant:
public class Zebra : Animal
{
public Zebra(string name) : base(name) { }
public static implicit operator Elephant(Zebra z)
{
return new Elephant(z.Name);
}
}
Instead, given the above conversion operator, the compiler will be able to change the type of the below array from Animal[]
to Elephant[]
, given that the Zebras can be now converted to a homogeneous collection of Elephants:
var compilerInferredAnimals = new []
{
new Zebra("Zed"),
new Elephant("Ellie")
};
Using Implicit Conversion Operators at run time
*As mentioned by Eric, the conversion operator can however be accessed at run time by resorting to dynamic
:
var mixedAnimals = new Animal[] // i.e. Polymorphic collection
{
new Zebra("Zed"),
new Elephant("Ellie")
};
foreach (dynamic animal in mixedAnimals)
{
castedAnimals.Add(animal);
}
// Returns Zed, Ellie