Skip to content

Implement both dispose and async dispose patterns: Dispose(false) from DisposeAsync() is not necessary #46349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
devdorn opened this issue May 22, 2025 · 0 comments

Comments

@devdorn
Copy link

devdorn commented May 22, 2025

Type of issue

Typo

Description

• Dispose(bool disposing) is called with true when the object is being disposed explicitly (by user code), and with false when called from a finalizer (to clean up unmanaged resources only).
• In the async dispose pattern, DisposeAsync() should call DisposeAsyncCore() to asynchronously release resources, then suppress finalization.
• There is no need to call Dispose(false) from DisposeAsync(), because:
• You are not in a finalizer.
• All resource cleanup should already be handled in DisposeAsyncCore().

The correct pattern for IAsyncDisposable is:

public async ValueTask DisposeAsync() { await DisposeAsyncCore().ConfigureAwait(false); GC.SuppressFinalize(this); }

Page URL

https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync

Content source URL

https://github.com/dotnet/docs/blob/main/docs/standard/garbage-collection/implementing-disposeasync.md

Document Version Independent Id

d72276e1-9059-815f-dc14-a03a7544ab87

Platform Id

cc69165e-7f9b-8c59-0b79-2ba09c791e8e

Article author

@IEvangelist

Metadata

  • ID: 61a961dc-3ebf-2a96-a6eb-2e0b62ab909f
  • PlatformId: cc69165e-7f9b-8c59-0b79-2ba09c791e8e
  • Service: dotnet-fundamentals

Related Issues

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

No branches or pull requests

1 participant