Skip to content

Commit 9d498cf

Browse files
authored
[UUF] Update example to match text (#44442)
1 parent ac8b832 commit 9d498cf

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

docs/standard/garbage-collection/implementing-dispose.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,31 @@ Method signatures are:
5454

5555
Because the `public`, non-virtual (`NotOverridable` in Visual Basic), parameterless `Dispose` method is called when it's no longer needed (by a consumer of the type), its purpose is to free unmanaged resources, perform general cleanup, and to indicate that the finalizer, if one is present, doesn't have to run. Freeing the actual memory associated with a managed object is always the domain of the [garbage collector](index.md). Because of this, it has a standard implementation:
5656

57-
:::code language="csharp" source="../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.disposable/cs/Disposable.cs" id="Dispose":::
58-
:::code language="vb" source="../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.disposable/vb/Disposable.vb" id="Dispose":::
57+
:::code language="csharp" source="./snippets/dispose-pattern/csharp/Disposable.cs" id="Dispose":::
58+
:::code language="vb" source="./snippets/dispose-pattern/vb/Disposable.vb" id="Dispose":::
5959

6060
The `Dispose` method performs all object cleanup, so the garbage collector no longer needs to call the objects' <xref:System.Object.Finalize%2A?displayProperty=nameWithType> override. Therefore, the call to the <xref:System.GC.SuppressFinalize%2A> method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call to <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> has no effect. The actual cleanup is performed by the `Dispose(bool)` method overload.
6161

6262
### The Dispose(bool) method overload
6363

6464
In the overload, the `disposing` parameter is a <xref:System.Boolean> that indicates whether the method call comes from a <xref:System.IDisposable.Dispose%2A> method (its value is `true`) or from a finalizer (its value is `false`).
6565

66-
:::code language="csharp" source="../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.disposable/cs/Disposable.cs" id="DisposeBool":::
67-
:::code language="vb" source="../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.disposable/vb/Disposable.vb" id="DisposeBool":::
66+
:::code language="csharp" source="./snippets/dispose-pattern/csharp/Disposable.cs" id="DisposeBool":::
67+
:::code language="vb" source="./snippets/dispose-pattern/vb/Disposable.vb" id="DisposeBool":::
6868

6969
> [!IMPORTANT]
70-
> The `disposing` parameter should be `false` when called from a finalizer, and `true` when called from the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method. In other words, it is `true` when deterministically called and `false` when non-deterministically called.
70+
> The `disposing` parameter should be `false` when called from a finalizer, and `true` when called from the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method. In other words, it is `true` when deterministically called and `false` when nondeterministically called.
7171
7272
The body of the method consists of three blocks of code:
7373

7474
- A block for conditional return if object is already disposed.
75-
- A block that frees unmanaged resources. This block executes regardless of the value of the `disposing` parameter.
7675
- A conditional block that frees managed resources. This block executes if the value of `disposing` is `true`. The managed resources that it frees can include:
7776

7877
- **Managed objects that implement <xref:System.IDisposable>.** The conditional block can be used to call their <xref:System.IDisposable.Dispose%2A> implementation (cascade dispose). If you have used a derived class of <xref:System.Runtime.InteropServices.SafeHandle?displayProperty=nameWithType> to wrap your unmanaged resource, you should call the <xref:System.Runtime.InteropServices.SafeHandle.Dispose?displayProperty=nameWithType> implementation here.
79-
8078
- **Managed objects that consume large amounts of memory or consume scarce resources.** Assign large managed object references to `null` to make them more likely to be unreachable. This releases them faster than if they were reclaimed nondeterministically.
8179

80+
- A block that frees unmanaged resources. This block executes regardless of the value of the `disposing` parameter.
81+
8282
If the method call comes from a finalizer, only the code that frees unmanaged resources should execute. The implementer is responsible for ensuring that the false path doesn't interact with managed objects that may have been disposed. This is important because the order in which the garbage collector disposes managed objects during finalization is nondeterministic.
8383

8484
## Cascade dispose calls

samples/snippets/csharp/VS_Snippets_CLR/conceptual.disposable/cs/Disposable.cs renamed to docs/standard/garbage-collection/snippets/dispose-pattern/csharp/Disposable.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ protected virtual void Dispose(bool disposing)
2323

2424
if (disposing)
2525
{
26-
// TODO: dispose managed state (managed objects).
26+
// Dispose managed state (managed objects).
27+
// ...
2728
}
2829

29-
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
30-
// TODO: set large fields to null.
30+
// Free unmanaged resources.
31+
// ...
3132

3233
_disposed = true;
3334
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.disposable/vb/Disposable.vb renamed to docs/standard/garbage-collection/snippets/dispose-pattern/vb/Disposable.vb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313

1414
' <SnippetDisposeBool>
1515
Protected Overridable Sub Dispose(disposing As Boolean)
16-
If disposed Then Exit Sub
16+
If disposed Then Exit Sub
1717

18-
' A block that frees unmanaged resources.
19-
2018
If disposing Then
21-
' Deterministic call…
22-
' A conditional block that frees managed resources.
23-
End If
24-
25-
disposed = True
19+
20+
' Free managed resources.
21+
' ...
22+
23+
End If
24+
25+
' Free unmanaged resources.
26+
' ...
27+
28+
disposed = True
2629
End Sub
2730
' </SnippetDisposeBool>
2831

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)