While implementing a design using nested generic collections, I stumbled across those limitations apparently caused by C#'s invariant Generics:
Cannot convert from 'Collection<subtype of T> to 'Collection<T>'
That means, the following will not work, apparently due to the invariance of Generics:
class Outer<TInner, TInnerItem> where TInner : Inner<TInnerItem>
{
public void Add(TInner item)
{
item.Outer = this; // ERROR:
// Cannot implicitly convert from Outer<TInner, TInnerItem>
// to Outer<Inner<TInnerItem>, TInnerItem>
}
}
class Inner<TInnerItem> : ICollection<TInnerItem>
{
Outer<Inner<TInnerItem>, TInnerItem> _outer;
public Outer<Inner<TInnerItem>, TInnerItem> Outer
{
set { _outer = value; }
}
}
(In the actual code, both Inner<>
and Outer<>
implement ICollection<>
.)
I need the Inner<>
objects to have a reference to its container collection in order to access some of its data.
How would you implement these nested collections, preferably using a generic approach as outlined above? How would you set the reference to the container collection in the Inner<>
class?
Cheers!
The language can't let you convert from Collection<subtype of T> to Collection<T>
Let me explain why.
Imagine you have a Collection<subT> and cast it to Collection<T>. That's ok, since subT inherits from T.
When you retrieve an object from
collection_of_T
, you're assured that it's ok.Later, add a
T
object tocollection_of_T
. Now you have, incollection_of_subT
an object which is not asubT
. Whoops.In order to achieve what you want, you must create a new Collection alltogether.
Which probably is not good for you. Do you really need the
TInner
generic argument in theOuter
class? Would it be possible to replace withInner<TInnerItem>
in the rest of the code instead?Introducing a (possibly abstract) base class which is not dependant on TInner may help you:
Or wait for C# 4.0, which introduces co/contra-variant generic interfaces.