The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

djordan (6) [Avatar] Offline
#1
I'm not sure I follow you in your last bullet point on p 104.

The sublocations list in the implementation is a List<FabulousStorageLocation> and so is also a IEnumerable<FabulousStorageLocation>.

GetSublocations returns IEnumerable<IStorageLocation>. Don't we want the helper method to convert an IEnumerable<TSource> to IEnumerable<TDest> (not IEnumerators) where in this case TSource is FabulousStorageLocation and TDest is IStorageLocation?

Dave
jon.skeet (483) [Avatar] Offline
#2
Re: p 104, last bullet point
Hi Dave,

You're right - you would want to convert an IEnumerable<TSource> to an IEnumerable<TDest>. In fact, with iterators you could do this without an explicit helper class, using an iterator block:

public IEnumerable<IStorageLocation> GetSublocations()
{
foreach (FabulousLocation location in list)
{
yield return location;
}
}

If you *did* write an explicit helper class, you could either use the same trick with iterator blocks, or use *another* helper class which really *did* convert from IEnumerator<TSource> to IEnumerator<TDest>.

I'm hoping to improve this whole section in the second edition - particularly as C# 4 *does* support variance in certain situations.

Jon