If I have the following classes:
public class MyItems : List<MyItem>
{
..
}
public class MyItem : Item
{
..
}
How could I go about casting an instance of MyItems back down to List<Item>? I’ve tried doing an explicit cast and I get an exception.
You can’t, because C# doesn’t support generic variance (see here for discussion of terminology), and even if it did, it wouldn’t allow this case, because if you could cast MyItems to
List<Item>, you could callAdd(someItemThatIsntAMyItem), which would violate type safety (because a MyItems can contain only MyItem objects, not arbitrary items).See this question (or search SO for “c# generic variance”) for additional information about this issue and future changes in C# 4 (though these will not affect your specific case).