0

I've that a C# array implements IEnumerable so the compiler has no complaints converting:

string[] array = new string[1];
IEnumerable<string> collection = array;

However when I have both wrapped in tasks, I get an error. Why is this not possible?

Task<string[]> arrayTask = Task.FromResult(new string[1]);
Task<IEnumerable<string>> collectionTask = arrayTask;

Here's the compile error I get (where T is the type):

Cannot implicitly convert type 'System.Threading.Tasks.Task<T[]>' to 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<T>>
teaMonkeyFruit
  • 543
  • 4
  • 16
  • 3
    One more time: a box of apples is not a box of fruit. Why not? Because you can put a banana into a box of fruit, but if you put a banana into a box of apples, it is not a box of apples anymore. Therefore those two types are different, even though an apple is a fruit. – Eric Lippert Apr 04 '20 at 06:21
  • Well I'm looking at some docs for a library and I'm confused because they are returning a Task in a method where the return type is a Task> – teaMonkeyFruit Apr 05 '20 at 00:25
  • Well then the documentation is probably wrong; report the bug in the documentation if you feel like it ought to be addressed. – Eric Lippert Apr 05 '20 at 00:48

0 Answers0