When implementing design using nested shared collections, I came across those restrictions that were apparently caused by C # invariants:
Cannot convert from 'Collection <subtype of T> to' Collection <T> '
This means that the following will not work, apparently due to Generics invariance:
class Outer<TInner, TInnerItem> where TInner : Inner<TInnerItem> { public void Add(TInner item) { item.Outer = this;
(In actual code, both Inner<> and Outer<> implement ICollection<> .)
I need Inner<> objects to have a link to its collection of containers in order to access some of my data.
How would you implement these nested collections, preferably using a generic approach, as described above? How would you set up a container collection reference in the Inner<> class?
Hooray!
amain source share