ValueTask consumed incorrectly (CA2012)
Ce contenu n’est pas encore disponible dans votre langue.
Key dotnet.async.valuetask-misuse · Severity error · Category async
Microsoft analyzer CA2012. Flags a ValueTask awaited twice, stored and awaited later, or read with .Result before it completes. A ValueTask may wrap a pooled object that is reused after the first await.
Incorrect
Section titled “Incorrect”ValueTask<int> pending = reader.ReadAsync(buffer);var first = await pending;var again = await pending;Correct
Section titled “Correct”var read = await reader.ReadAsync(buffer);// need it twice? keep the result, or call .AsTask() once