You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
privateSafeHandleresource; // handle to a resource
60
60
61
-
public DisposableResourceHolder(){
61
+
publicDisposableResourceHolder(){
62
62
this.resource=... // allocates the resource
63
63
}
64
64
65
-
public void Dispose(){
65
+
publicvoidDispose(){
66
66
Dispose(true);
67
67
GC.SuppressFinalize(this);
68
68
}
69
69
70
-
protected virtual void Dispose(bool disposing){
71
-
if (disposing){
70
+
protectedvirtualvoidDispose(booldisposing){
71
+
if (disposing){
72
72
if (resource!=null) resource.Dispose();
73
73
}
74
74
}
@@ -83,9 +83,9 @@ public class DisposableResourceHolder : IDisposable {
83
83
84
84
All resource cleanup should occur in this method. The method is called from both the finalizer and the `IDisposable.Dispose` method. The parameter will be false if being invoked from inside a finalizer. It should be used to ensure any code running during finalization is not accessing other finalizable objects. Details of implementing finalizers are described in the next section.
// now call some native methods using the resource
159
-
...
159
+
...
160
160
}
161
-
protected virtual void Dispose(bool disposing){
162
-
if(disposed) return;
161
+
protectedvirtualvoidDispose(booldisposing){
162
+
if(disposed) return;
163
163
// cleanup
164
164
...
165
165
disposed=true;
@@ -171,12 +171,12 @@ public class DisposableResourceHolder : IDisposable {
171
171
172
172
When doing so, it is important that you make the `Close` implementation identical to `Dispose` and consider implementing the `IDisposable.Dispose` method explicitly.
173
173
174
-
```
174
+
```csharp
175
175
publicclassStream : IDisposable {
176
-
IDisposable.Dispose(){
176
+
IDisposable.Dispose(){
177
177
Close();
178
178
}
179
-
public void Close(){
179
+
publicvoidClose(){
180
180
Dispose(true);
181
181
GC.SuppressFinalize(this);
182
182
}
@@ -195,29 +195,29 @@ public class Stream : IDisposable {
195
195
196
196
The following code shows an example of a finalizable type:
197
197
198
-
```
198
+
```csharp
199
199
publicclassComplexResourceHolder : IDisposable {
200
200
201
201
privateIntPtrbuffer; // unmanaged memory buffer
202
202
privateSafeHandleresource; // disposable handle to a resource
0 commit comments