Skip to content

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

Open
@devdorn

Description

@devdorn

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions