I know its possible to cast a list of items from one type to another (given that your object has a public static explicit operator method to do the casting) one at a time as follows:
List<Y> ListOfY = new List<Y>();
foreach(X x in ListOfX)
ListOfY.Add((Y)x);
But is it not possible to cast the entire list at one time? For example,
ListOfY = (List<Y>)ListOfX;
If X
can really be cast to Y
you should be able to use
List<Y> listOfY = listOfX.Cast<Y>().ToList();
Some things to be aware of (H/T to commenters!)
using System.Linq;
to get this extension methodList<Y>
will be created by the call to ToList()
.