I have this code:
static List<string> lst = new List<string>();
public static Task<IEnumerable<String>> GetStrings() {
IEnumerable<String> x = lst;
// This line below compiles just fine.
// return Task.Run(() => x);
// This line gives a compiler error:
// Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>' to
// 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<string>>'
return Task.Run(() => lst);
}
Why does this line compile just fine: return Task.Run(() => x);
while return Task.Run(() => lst);
gives the compiler error:
Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List>' to 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable>'
I would think that since List<string>
is an IEnumerable<string>
, I should not have to explicitly set x
. I mean, the compiler knows lst
is an IEnumerable<string>
.
I think I must be missing something fundamental here.