The return value of your EnumerateAs()
should be IEnumerable<T>
, where T is the type of object contained in your collection. I recommend reading more about yield return, as this may help you to understand how enumeration works. There is no default class for providing an enumeration 'strategy', but you can easily implement something like this by using yield return on the underlying collection in various ways.
It's not clear from your question exactly how the enumeration strategies would interact with your collection class. It looks like you might be after something like:
public interface IEnumerationStrategy<TCollection, T>
{
IEnumerable<T> Enumerate(TCollection source);
}
public class Quark {}
public class MyCollection
{
public IEnumerable<Quark> EnumerateAs(IEnumerationStrategy<MyCollection, Quark> strategy)
{
return strategy.Enumerate(this);
}
//Various special methods needed to implement stategies go here
}
public class SpecialStrategy: IEnumerationStrategy<MyCollection, Quark>
{
public IEnumerable<Quark> Enumerate(MyCollection source)
{
//Use special methods to do custom enumeration via yield return that depends on specifics of MyCollection
}
}
Note that you might also replace the strategy class with a simple strategy Func<MyCollection, IEnumerable<T>>
, but the above matches your desired syntax most closely.