Skip to content
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

Added alert regarding the responsibility of the GC as it relates to implementations of IDisposable #24259

Merged
merged 10 commits into from
May 19, 2021
Prev Previous commit
Apply suggestions from code review
Co-authored-by: Genevieve Warren <[email protected]>
  • Loading branch information
IEvangelist and gewarren committed May 19, 2021
commit 069e9bfd459195870065ffbfd8db2a8352b3315e
4 changes: 2 additions & 2 deletions docs/standard/garbage-collection/using-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ms.assetid: 81b2cdb5-c91a-4a31-9c83-eadc52da5cf0

# Using objects that implement IDisposable

The common language runtime's garbage collector (GC) reclaims the memory used by managed objects. Typically, types that use unmanaged resources implement the <xref:System.IDisposable> or <xref:System.IAsyncDisposable> interface to allow the resources needed by unmanaged resources to be reclaimed. When you finish using an object that implements <xref:System.IDisposable>, you call the object's <xref:System.IDisposable.Dispose%2A> or <xref:System.IAsyncDisposable.DisposeAsync%2A> implementation to explicitly perform cleanup. You can do this in one of two ways:
The common language runtime's garbage collector (GC) reclaims the memory used by managed objects. Typically, types that use unmanaged resources implement the <xref:System.IDisposable> or <xref:System.IAsyncDisposable> interface to allow the unmanaged resources to be reclaimed. When you finish using an object that implements <xref:System.IDisposable>, you call the object's <xref:System.IDisposable.Dispose%2A> or <xref:System.IAsyncDisposable.DisposeAsync%2A> implementation to explicitly perform cleanup. You can do this in one of two ways:

- With the C# `using` statement or declaration (`Using` in Visual Basic).
- By implementing a `try/finally` block, and calling the <xref:System.IDisposable.Dispose%2A> or <xref:System.IAsyncDisposable.DisposeAsync%2A> method in the `finally`.
Expand All @@ -25,7 +25,7 @@ The common language runtime's garbage collector (GC) reclaims the memory used by
> - [Implement a Dispose method](implementing-dispose.md)
> - [Implement a DisposeAsync method](implementing-disposeasync.md)

Objects that implement <xref:System.IDisposable?displayProperty=fullName> or <xref:System.IAsyncDisposable?displayProperty=fullName> should always be properly disposed of, regardless of variable scoping, unless otherwise explicitly stated. Objects that define a finalizer usually call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> with an argument of `this` from either their `Dispose` or DisposeAsync` implementation. This is to ensure that when their cleanup functions are called, they indicate the finalizer need not run.
Objects that implement <xref:System.IDisposable?displayProperty=fullName> or <xref:System.IAsyncDisposable?displayProperty=fullName> should always be properly disposed of, regardless of variable scoping, unless otherwise explicitly stated. Types that define a finalizer to release unmanaged resources usually call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> from either their `Dispose` or `DisposeAsync` implementation. Calling <xref:System.GC.SuppressFinalize%2A> indicates to the GC that the finalizer has already been run and the object shouldn't be promoted for finalization.

## The using statement

Expand Down