简体   繁体   English

为什么这个async / await代码生成“...并非所有代码路径都返回一个值”?

[英]Why does this async / await code generate “…not all code paths return a value”?

Hopefully this isn't a repeat, but there are 5000+ questions here with "not all code paths return a value"! 希望这不是重复,但这里有5000多个问题,“并非所有代码路径都返回值”!

Quite simply, why does this method with a non-generic implementation compile just fine: 很简单,为什么这个非泛型实现的方法编译得很好:

    public static async Task TimeoutAfter(this Task task, int millisecondsTimeout)
    {
        if (task == await Task.WhenAny(task, Task.Delay(millisecondsTimeout)))
            await task;
        else
            throw new TimeoutException();
    }

while this attempt to make the method generic generates a Return state missing / ... not all code paths return a value warning / error?: 虽然这种使方法通用的尝试会生成一个Return state missing / ... not all code paths return a value警告/错误?:

    public static async Task<T> TimeoutAfter<T>(this Task<T> task, int millisecondsTimeout)
    {
        if (task == await Task.WhenAny(task, Task.Delay(millisecondsTimeout)))
            await task;
        else
            throw new TimeoutException();
    }

The non-generic Task type is somewhat equivalent to an awaitable void method. 非泛型Task类型在某种程度上等同于一个等待的void方法。 Just like a void method, you can't return anything from a method that has a return type of Task , which is why the first example compiles. 就像void方法一样,您不能从返回类型为Task的方法返回任何内容,这就是第一个示例编译的原因。 The second example, though, expects a return value of the generic type and you aren't providing one in the path where you await another call. 但是,第二个示例需要泛型类型的返回值,并且您没有在等待另一个调用的路径中提供一个。

Quoting from the MSDN reference on the async keyword, specifically about return types. 引用async关键字上的MSDN引用 ,特别是有关返回类型的引用

You use Task if no meaningful value is returned when the method is completed. 如果方法完成时没有返回有意义的值,则使用Task。 That is, a call to the method returns a Task, but when the Task is completed, any await expression that's awaiting the Task evaluates to void. 也就是说,对该方法的调用返回一个Task,但是当Task完成时,任何等待Task的await表达式都会返回void。

In the second example you gave you didn't return anything. 在第二个例子中,你给了你没有返回任何东西。 (See Chris Hannon's answer for why). (参见Chris Hannon的回答为什么)。

public static async Task<T> TimeoutAfter<T>(this Task<T> task, int millisecondsTimeout) {
    if (task == await Task.WhenAny(task, Task.Delay(millisecondsTimeout)))
        return await task; // return the Task of T
    else
        throw new TimeoutException();
}

In addition to what @ChrisHannon said, from the await documentation . 除了@ChrisHannon所说的,还有等待文档

... if await is applied to the result of a method call that returns a Task<TResult> , then the type of the await expression is TResult . ...如果将await应用于返回Task<TResult>的方法调用的结果,则await表达式的类型为TResult If await is applied to the result of a method call that returns a Task , then the type of the await expression is void . 如果将await应用于返回Task的方法调用的结果,则await表达式的类型为void ... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM