I have the following:
public interface ICartItem
{
string Name { get; set; }
}
public class CartItem : ICartItem
{
public string Name { get; set; }
}
I then create a List and cast it to an interface:
IList<CartItem> items = new List<CartItem>()
{
new CartItem() { Name = "MyItem" }
};
IList<ICartItem> cartItems = items.Cast<ICartItem>().ToList();
Is there a way to cast it back again like illustrated below?
IList<CartItem> newList = cartItems as IList<CartItem>;
Do you need a copy of the list?
If yes, and you are sure that there are only CartItems within the list you can do it with
But i think you’d like it a little more robust. In that case you can try it with
But i think you need to create a new list. I can’t think of a way to work on the same with an
IListinterface (IEnumerable<T>works as shown above).