views:

44

answers:

1

Hi to all. Here's the situation: There's a base class of type BaseObject ,from which all other classes are derived(i.e. Task : BaseObject). There's also a base class collection of type BaseObjectCollection which inherits from Dictionary<ulong,BaseObject> and some other collections that inherit from it (i.e. TaskCollection:BaseObjectCollection). When iterating over TaskCollection members they are shown as BaseObject type. Is there any way to make the collection "change" its values' types so that it will possess the functionality of BaseobjectCollection and yet appear as Dictionary<ulong,Task> instead of Dictionary<ulong,BaseObject>

+1  A: 

You should change your BaseObjectCollection type to be generic in the type of value it will use. For example:

public class BaseObjectCollection<T> : Dictionary<ulong, T>
    where T : BaseObject

Then you can use a BaseObjectCollection<Task> and still be completely type-safe.

(I would personally use composition in most cases rather than deriving a new type from Dictionary<,> but that's a different matter.)

Jon Skeet
Thanks a lot ,that was an eyeopening for me, as I'm only a newbie to the generic types.By the way, why do you prefer composition rather that using Dictionary?
Gena Verdel