N
Niels Ull
Hi!
I have a generic abstract base class MyCollection<T> which represents a collection
of T's with some common utility methods.
I then have a number of non-generic subclasses, e.g.
class FooCollection : MyCollection<Foo>
{ /* utility methods only for foo collections */ }
class BarCollection : MyCollection<Bar>
{ /* utility methods only for Bar collections */ }
Now, I'd like to provide a subset method in MyCollection - e.g. for getting
the first 10 items
(In real life selecting the subset is more complex and based on the structure
of MyCollection as well as properties of the T base type)
FooCollection allFoos = ...
FooCollection tenFoos = allFoos.GetSubset(10);
But I'd like to return a FooCollection - not a MyCollection<Foo>. The best
I have been able to come up is this method in MyCollection:
DERIVEDCOLLECTION GetSubset<DERIVEDCOLLECTION>(int count) where DERIVEDCOLLECTION:
MyCollection<T>, new()
{
DERIVEDCOLLECTION result = new DERIVEDCOLLECTION();
for(int i = 0; i < count; i++) // example logic only!
result.Add(this);
return result;
}
This is not very elegant, since it requires the user to specify the target
class:
FooCollection tenFoos = allFoos.GetSubset<FooCollection>(10);
An alternative would be to return an IEnumerable<T>, but that would require
the user to write
FooCollection allFoos = ...
FooCollection tenFoos = new FooCollection();
tenFoos.AddAll(allFoos.GetSubset(10))
Or, finally, I could implement new GetSubset in all derived collections -
but since there are a number of such functions, that's not too useful either.
Does anybody have any suggestions?
I have a generic abstract base class MyCollection<T> which represents a collection
of T's with some common utility methods.
I then have a number of non-generic subclasses, e.g.
class FooCollection : MyCollection<Foo>
{ /* utility methods only for foo collections */ }
class BarCollection : MyCollection<Bar>
{ /* utility methods only for Bar collections */ }
Now, I'd like to provide a subset method in MyCollection - e.g. for getting
the first 10 items
(In real life selecting the subset is more complex and based on the structure
of MyCollection as well as properties of the T base type)
FooCollection allFoos = ...
FooCollection tenFoos = allFoos.GetSubset(10);
But I'd like to return a FooCollection - not a MyCollection<Foo>. The best
I have been able to come up is this method in MyCollection:
DERIVEDCOLLECTION GetSubset<DERIVEDCOLLECTION>(int count) where DERIVEDCOLLECTION:
MyCollection<T>, new()
{
DERIVEDCOLLECTION result = new DERIVEDCOLLECTION();
for(int i = 0; i < count; i++) // example logic only!
result.Add(this);
return result;
}
This is not very elegant, since it requires the user to specify the target
class:
FooCollection tenFoos = allFoos.GetSubset<FooCollection>(10);
An alternative would be to return an IEnumerable<T>, but that would require
the user to write
FooCollection allFoos = ...
FooCollection tenFoos = new FooCollection();
tenFoos.AddAll(allFoos.GetSubset(10))
Or, finally, I could implement new GetSubset in all derived collections -
but since there are a number of such functions, that's not too useful either.
Does anybody have any suggestions?