Can someone please explain why I am getting a compile error asking for an explicit cast? Since I’m constraining the output, I thought this would be allowed without any brute force.
** THANK YOU EVERYONE. IT WAS CONFUSING THE WAY I PRESENTED THE QUESTION. **
public interface IQuery<TInput, TOutput>
{
TOutput Execute(TInput input);
}
public abstract class PagedQuery<TInput, TOutput> : IQuery<TInput, TOutput>
where TOutput : IEnumerable<TOutput>
{
public TOutput Execute(TInput input)
{
return Enumerable.Empty<TOutput>(); // error here..
}
}
Enumerable.Empty<TOutput>()returns an implementation ofIEnumerable<TOutput>. Just becauseTOutputalso implementsIEnumerable<TOutput>doesn’t mean that you can convert anyIEnumerable<TOutput>value toTOutput.I agree with the commentators who say that constraining
TOutputto return a sequence of itself is pretty odd, by the way.I suspect you actually want something like this:
That makes a lot more sense to me.
EDIT: I’ve renamed the type parameter to make it clearer. So for
IQuery<,>,TOutput=IEnumerable<TElement>– so aPagedQueryreturns a sequence of elements, not a sequence of pages, each of which itself is a sequence of pages, each of which itself is a sequence of pages ad infinitum.