Nested generic collections: How to implement reference from item to container?
问题 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