On the previous episode…

Lastly, I have written about ContinueWith. Now it is time to show real case.

Case

Consider API;

public Task<IEnumerable<Foo>> GetFoos()
{
    return context.Foos.ToListAsync();
}

This won’t compile - Task<List<Foo>> cannot be casted into Task<IEnumerable<Foo>>. So how to handle it without introducing await/async?

public Task<IEnumerable<Foo>> GetFoos()
{
    return context.Foos.ToListAsync()
                       .ContinueWith(task => task.Result.AsEnumerable());
}

Patryk Wąsiewicz

A very casual blog about programmer's life.