tags:

views:

98

answers:

3

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.

+8  A: 

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 call Add(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).

itowlson
Thanks for the explanation. I figured out a different design to get what I needed.
Jeremy
+1, nice link, ty
Rubens Farias
A: 

Hey,

I believe I saw that was coming in 4.0. Not available yet.

Brian
Nope, won't be coming in 4.0 because (a) C# 4 supports variance only for delegates and interfaces, not classes (sigh) and (b) even if it did support variance on classes, it wouldn't be safe for `List<T>` because T is in-out (variance is safe only if the type parameter appears only as inputs or only as outputs, not if it appears as both).
itowlson
A: 
public class MyList : IList<MyClass>
{
    List<MyClass> _list;


    //Implement all IList members like so
    public int IndexOf(MyClass item)
    {
        return _list.IndexOf(item);
    }


    //Then help the type system a bit with these two static methods.
    public static implicit operator List<MyClass> (MyList mylist)
    {
        return mylist._list;
    }

    public static implicit operator MyList (List<MyClass> list)
    {
        return new MyList() { _list = list;}
    }
Rodrick Chapman