Suppose the following code:
private async Task Test1Async() => await Task.Delay(1000).ConfigureAwait(false);
private Task Test2Async() => Test1Async();
Functionally, these functions are exactly the same but the compiler treats calling these methods different. The following code compiles, but issues a CS4014 warning:
private void Test() => Test1Async(); // CS4014 is shown
It generates the warning "because this call is not awaited, the current method continues to run before the call is completed". This is a proper warning, because it often indicates a flaw in your code. In case you actually want this behavior, then you can solve it by using the following code:
private void Test() => _ = Test1Async(); // CS4014 is not shown anymore
Assigning the value to _
is a relative new feature to indicate that the value is ignored intentionally.
This code doesn't raise CS4014:
private void Test() => Test2Async(); // CS4014 is not shown!
Of course, I could rewrite all my methods to use the async/await
method, but this results in more code that runs less efficient (due to the state machine generated by the async
keyword). Maybe I will never forget about it, but my coworkers might and then I won't get a trigger (or I call a third-party library that doesn't use async).
There is also a difference in the warning about the returned Task usage.
Does anyone know why this warning is not generated for methods that return a Task
that don't use the async
keyword?
Async methods that don't contain a return statement or that contain a return statement that doesn't return an operand usually have a return type of Task. Such methods return void if they run synchronously.
Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.
When a asynchronous method is executed, the code runs but nothing happens other than a compiler warning. What is most likely causing the method to not return anything? (A) The return yield statement is missing at the end of the method.
@Servy async creates a state machine that manages any awaits within the async method. If there are no await s within the method it still creates that state machine--but the method is not asynchronous. And if the async method returns void , there's nothing to await. So, it's more than just awaiting a result.
Does anyone know why this warning is not generated for methods that return a
Task
that don't use theasync
keyword?
Probably to avoid spurious warnings on legacy code. Task
predated async
, and there's a number of methods out there that return Task
but don't have anything to do with asynchronous code.
Personally, I think ignoring a Task
return value is almost certainly a mistake, even in non-asynchronous code. But I assume that the MS team ran metrics and determined this warning would be too noisy for those kinds of code bases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With