Skip to content

Instantly share code, notes, and snippets.

@MattParkerDev
Created August 3, 2025 04:17
Show Gist options
  • Select an option

  • Save MattParkerDev/d9bd0be8a5619996b81780be4c6e566f to your computer and use it in GitHub Desktop.

Select an option

Save MattParkerDev/d9bd0be8a5619996b81780be4c6e566f to your computer and use it in GitHub Desktop.
Correct IAsyncDisposable.ConfigureAwait(false) usages
// 'using' statement
// if you don't need access to the CancellationTokenRegistration
await using (cancellationToken.Register(() => buildManager.CancelAllSubmissions()).ConfigureAwait(false))
{
// disposed outside this scope
}
// 'using' statement
// if you need access to the CancellationTokenRegistration
var test = cancellationToken.Register(() => buildManager.CancelAllSubmissions());
await using (test.ConfigureAwait(false))
{
// disposed outside this scope
var cts = test.Token;
};
// 'using' declaration
// if you don't need access to the CancellationTokenRegistration
await using var _ = cancellationToken.Register(() => buildManager.CancelAllSubmissions()).ConfigureAwait(false);
// 'using' declaration
// if you need access to the CancellationTokenRegistration
var test2 = cancellationToken.Register(() => buildManager.CancelAllSubmissions());
await using var __ = test2.ConfigureAwait(false);
var cts2 = test2.Token;
@MattParkerDev
Copy link
Author

Noting that await using var _ and await using var __ are not true discards, they are variables, hence the need to have different names. There is a discussion around allowing await using _ = ... here: dotnet/csharplang#8605

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment