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

Task<T> is simply not a covariant type.

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

In #3, we have:

return await Task.FromResult(doctors)

Which is the same as:

return await Task.FromResult<List<DoctorDto>>(doctors)

Which is the same as:

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

This works because List<DoctorDto> can be converted IEnumerable<DoctorDto>.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)