Why async / await allows for implicit conversion from a List to IEnumerable?

前端 未结 3 1768
野趣味
野趣味 2021-02-12 03:47

I\'ve just been playing around with async/await and found out something interesting. Take a look at the examples below:

// 1) ok - obvious
public Task

        
3条回答
  •  一向
    一向 (楼主)
    2021-02-12 03:59

    Task is simply not a covariant type.

    Although List can be converted to IEnumerable, Task> cannot be converted to Task>. And In #4, Task.FromResult(doctors) returns Task>.

    In #3, we have:

    return await Task.FromResult(doctors)
    

    Which is the same as:

    return await Task.FromResult>(doctors)
    

    Which is the same as:

    List result = await Task.FromResult>(doctors);
    return result;
    

    This works because List can be converted IEnumerable.

提交回复
热议问题