Exercise: Generic Catalog, GetAll(Predicate p)

In this exercise you will use a Predicate delegate and compare it to an interface.

Predicate

In the Generic Catalog exercise add a method

The GetAll(...) method must return and object of either type IList<TElement>, ICollection<TElement>, or IEnumerable<TElement>.

In the method loop through all the elements in the catalog, and for each element apply the predicate. If the predicate returns true, the element should be in the output.

Unit test the method

Interface

Make a generic interface ICondition<T>. The interface should have one method

In the Generic catalog make yet another method

GetAll(ICondition<TElement> predicate)

The method must return and object of either type IList<TElement>, ICollection<TElement>, or IEnumerable<TElement>.

In the method loop through all the elements in the catalog, and for each element apply the predicate. If the predicate returns true, the element should be in the output.

Unit test the method. To do the Unit Test you first have to create a class that implements the interface ICondition<T>.

Compare: Delegate vs. single-method interface

This is a theoretical question (no programming needed) regarding delegates vs. single-method interface

Extra: Interface → Delegate

Add a read-only property to the interface ICondition<T>

The property must return a Predicate which is equivalent to the interface.

Hint: It is very easy to implement the property ...

You might even want to make an abstract class AbstractCondition<T> : ICondition<T>, which implements the property (but not the Evaluate method).