Generic interface implementing multiple generic types - how to share a
method implementation?
Let's say I have the following interface
public interface IFilter<T>
{
IEnumerable<T> ApplyFilter(IEnumerable<T> list);
}
And a specific implementation like this:
public class PetFilter: IFilter<Dog>, IFilter<Cat>
{
public IEnumerable<Dog> ApplyFilter(IEnumerable<Dog> list)
{
return ApplyFilter<Dog>(list);
}
public IEnumerable<Cat> ApplyFilter(IEnumerable<Cat> list)
{
return ApplyFilter<Dog>(list);
}
private IEnumerable<T> ApplyFilter<T>(IEnumerable<T> list)
{
// do the work here
}
}
Is there any way to avoid having to implement separate methods for both
Dog and Cat, given that they share the same implementation?
No comments:
Post a Comment