So i was digging through some old posts of Adam’s and found one where he’s pointing to article on MSDN on the new language features in C# 3.0.
It got me thinking about how you can bring some of those features down to C# 2.0 and I came up with the concept of a RichEnumerable.
This type can be used for querying using specifications.
The interface implements the IEnumerable
public interface IRichEnumerable< T > : IEnumerable< T >
{
IEnumerable< T > Where( ISpecification< T > criteria );
}
The implementation so far looks like this.
internal class RichEnumerable< T > : IRichEnumerable< T >
{
private readonly IEnumerable< T > _items;
public RichEnumerable( IEnumerable< T > items )
{
_items = items;
}
public IEnumerable< T > Where( ISpecification< T > criteria )
{
foreach( T item in _items )
{
if( criteria.IsSatisfiedBy( item ) )
{
yield return item;
}
}
}
IEnumerator< T > IEnumerable< T >.GetEnumerator( )
{
return _items.GetEnumerator( );
}
public IEnumerator GetEnumerator( )
{
return ( ( IEnumerable< T > )this ).GetEnumerator( );
}
}
This could be extended further to return a RichEnumerable
Just a quick lunch time idea… source is provided.