0

I've got two base classes:

class BaseCollection<T> where T : BaseCollectionItem
{

}

and

class BaseCollectionItem
{

}

These are base classes, which are later subclassed in the following way:

class MyCollection : BaseCollection<MyItem>
{

}

class MyItem : BaseCollectionItem
{

}

I want to store a reference to collection, which holds an item in a BaseCollectionItem. How can I do it?

class BaseCollectionItem
{
    private ??? ownerCollection;
}
Spook
  • 25,318
  • 18
  • 90
  • 167
  • 1
    I'm confused by your last sentence. Do you want the ??? to be replaced with BaseCollection? – Warty Oct 15 '13 at 09:47
  • i don't think that it is required. Collection should have reference to the items – Rajesh Subramanian Oct 15 '13 at 09:47
  • @ItzWarty I cannot. When you subclass `BaseCollection` and `BaseCollectionItem`, the first is no longer specialized by `BaseCollectionItem`. Instead, it becames `BaseCollection`. So your solution won't work, unfortunately :( – Spook Oct 15 '13 at 09:50
  • @RajeshSubramanian And in my specific situation I actually *need* the items to have a reference to the collection, which owns them. Otherwise I wouldn't ask this question. – Spook Oct 15 '13 at 09:52
  • why don't you change your class definition of MyCollection to this: class MyCollection : BaseCollection ? – MUG4N Oct 15 '13 at 10:03
  • @MUG4N Because in my specific situation collection has to be aware of the specific type it contains (it instantiates it sometimes, actually) – Spook Oct 15 '13 at 10:07

2 Answers2

0

If you're looking for BananaCollection(BaseCollection) to always contain Bananas and you want Banana to always be in a BananaCollection (BaseCollection), then you have a case of circular generic type parameters.

You can check out circular generic type parameters for more info.

Community
  • 1
  • 1
Warty
  • 7,237
  • 1
  • 31
  • 49
0

It looks like you want covariance on the type of your field, which cannot be done cleanly.

You could try to do it using the Curiously Recurring Generic Pattern as described by Eric Lippert here, but beware of the problems!

And I don't just mean the problems described in said article, but some you introduce yourself as well. For example: why would you not want a Giraffe to be stored in a BaseCollection<Mammal> ?

Kris Vandermotten
  • 10,111
  • 38
  • 49
  • Because BaseCollection sometimes instantiates its children and has to know precisely, which items does it contain. It's generic, so if I want to keep items only of specific type, I just have to subclass it and it takes care of all reading and writing and accessing items. – Spook Oct 15 '13 at 10:27
  • Let me rephrase that: why would you make it so that it is not allowed for a `BaseCollection` to contain, amonst other mammals, a mammal that happens to be a `Giraffe` ? – Kris Vandermotten Oct 15 '13 at 15:12
  • this is a matter of requirements for the structure. There is a dozen of different collections and each one is responsible for keeping only one (sealed, final) class. On the other side, each derives from the base class and every one has to be aware of precise type of the contained items. – Spook Oct 16 '13 at 07:39
  • Please note, this is a question about technical aspect of C#, not about specifying requirements for the data structure :) – Spook Oct 16 '13 at 07:39