public interface IItem { }
public class AuctionItem : IItem { }
public class BuyNowItem : IItem { }
public interface IResult<out T> where T : IItem {
IList<IItem> Items { get; set; }
int TotalCount { get; set; }
}
public class SearchResult<T> : IResult<T> where T : IItem
{
public IList<IItem> Items { get; set; }
public int TotalCount { get; set; }
}
You are unable to return a SearchResult<AuctionItem>
from DoSearches()
because then this would be possible:
List<BuyNowItem> items = new List<SearchResult<BuyNowItem>>();
items.Add(DoSearches()); // Could be a SeachResult<AuctionItem>
What does the interface IResult
do?
<out T>
declares that any type implementingIResult
will haveT
as an output. Consequently, you can do something likeIResult<IItem> result = DoSearches()
and it be valid.IList<IItem>
instead ofIList<T>
because the compiler can't prove that everyT
would be a type safe conversion toIItem
. The fuller explanation of this would require someone which much more expertise than me.
A new DoSearches()
could conceptually be this:
private IResult<IItem> DoSearches(Notification notification) {
var searchresult = _ebaySearchService.SearchAsync(auctionSearchOptions);
return searchresult;
}