async / await:无法隐式转换类型' void'到' int []'

时间:2017-12-11 06:30:59

标签: c#

我正在使用C#7.1及以下async / await代码给我错误,Cannot implicitly convert type 'void' to 'int[]'

它的解决方案是什么?

static async Task Main(string[] args)
    {
        Task task1 = Task.FromResult(3);
        Task task2 = Task.FromResult(5);
        Task task3 = Task.FromResult(7);

        int[] results = await Task.WhenAll(task1, task2, task3);

        Console.ReadLine();
    }

我想返回

之类的结果

// "results" contains { 3, 5, 7 }

1 个答案:

答案 0 :(得分:2)

以下是必须的:

Task<int> task1 = Task.FromResult(3);
Task<int> task2 = Task.FromResult(5);
Task<int> task3 = Task.FromResult(7);

int[] results = await Task.WhenAll(task1, task2, task3);

您正在将Task<int>投射到基本类型Task