Why is CS4014 not shown for all functions returning a task?

Assume 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 considers calling these methods different. The following code compiles, but issues warning CS4014:

private void Test() => Test1Async();   // CS4014 is shown

It generates a warning "because this call is not expected, the current method continues to be executed until the call ends." This is a valid warning, because it often indicates a flaw in your code. If you really want this behavior, you can solve it using the following code:

private void Test() => _ = Test1Async();   // CS4014 is not shown anymore

Assigning a value _is a relative new function, indicating that the value is ignored intentionally.

This code does not raise CS4014:

private void Test() => Test2Async();   // CS4014 is not shown!

, , async/await, , (- , async). , , , ( , async).

.

- , , Task, async?

+4
2

- , , Task, async?

, . Task predated async, , Task, .

, Task . , MS , .

+3

, Task. Task, . , , . .

; , , , , , ( ) , , , .

+1

Source: https://habr.com/ru/post/1683431/


All Articles