Skip to content

Commit 23af484

Browse files
yauhenipakalaRon Petrusha
authored andcommitted
Update dispose-pattern.md (#6114)
* Add C# code highlighting * Fix whitespace
1 parent f64544f commit 23af484

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

docs/standard/design-guidelines/dispose-pattern.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,22 @@ All programs acquire one or more system resources, such as memory, system handle
5353

5454
The following example shows a simple implementation of the basic pattern:
5555

56-
```
56+
```csharp
5757
public class DisposableResourceHolder : IDisposable {
5858

5959
private SafeHandle resource; // handle to a resource
6060
61-
public DisposableResourceHolder(){
61+
public DisposableResourceHolder() {
6262
this.resource = ... // allocates the resource
6363
}
6464

65-
public void Dispose(){
65+
public void Dispose() {
6666
Dispose(true);
6767
GC.SuppressFinalize(this);
6868
}
6969

70-
protected virtual void Dispose(bool disposing){
71-
if (disposing){
70+
protected virtual void Dispose(bool disposing) {
71+
if (disposing) {
7272
if (resource!= null) resource.Dispose();
7373
}
7474
}
@@ -83,9 +83,9 @@ public class DisposableResourceHolder : IDisposable {
8383

8484
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.
8585

86-
```
87-
protected virtual void Dispose(bool disposing){
88-
if (disposing){
86+
```csharp
87+
protected virtual void Dispose(bool disposing) {
88+
if (disposing) {
8989
if (resource!= null) resource.Dispose();
9090
}
9191
}
@@ -95,7 +95,7 @@ protected virtual void Dispose(bool disposing){
9595

9696
The call to `SuppressFinalize` should only occur if `Dispose(true)` executes successfully.
9797

98-
```
98+
```csharp
9999
public void Dispose(){
100100
Dispose(true);
101101
GC.SuppressFinalize(this);
@@ -106,17 +106,17 @@ public void Dispose(){
106106

107107
The `Dispose(bool)` method is the one that should be overridden by subclasses.
108108

109-
```
109+
```csharp
110110
// bad design
111111
public class DisposableResourceHolder : IDisposable {
112-
public virtual void Dispose(){ ... }
113-
protected virtual void Dispose(bool disposing){ ... }
112+
public virtual void Dispose() { ... }
113+
protected virtual void Dispose(bool disposing) { ... }
114114
}
115115

116116
// good design
117117
public class DisposableResourceHolder : IDisposable {
118-
public void Dispose(){ ... }
119-
protected virtual void Dispose(bool disposing){ ... }
118+
public void Dispose() { ... }
119+
protected virtual void Dispose(bool disposing) { ... }
120120
}
121121
```
122122

@@ -126,13 +126,13 @@ public class DisposableResourceHolder : IDisposable {
126126

127127
**✓ DO** allow the `Dispose(bool)` method to be called more than once. The method might choose to do nothing after the first call.
128128

129-
```
129+
```csharp
130130
public class DisposableResourceHolder : IDisposable {
131131

132132
bool disposed = false;
133133

134-
protected virtual void Dispose(bool disposing){
135-
if(disposed) return;
134+
protected virtual void Dispose(bool disposing) {
135+
if (disposed) return;
136136
// cleanup
137137
...
138138
disposed = true;
@@ -148,18 +148,18 @@ public class DisposableResourceHolder : IDisposable {
148148

149149
**✓ DO** throw an <xref:System.ObjectDisposedException> from any member that cannot be used after the object has been disposed of.
150150

151-
```
151+
```csharp
152152
public class DisposableResourceHolder : IDisposable {
153153
bool disposed = false;
154154
SafeHandle resource; // handle to a resource
155155
156-
public void DoSomething(){
157-
if(disposed) throw new ObjectDisposedException(...);
156+
public void DoSomething() {
157+
if (disposed) throw new ObjectDisposedException(...);
158158
// now call some native methods using the resource
159-
...
159+
...
160160
}
161-
protected virtual void Dispose(bool disposing){
162-
if(disposed) return;
161+
protected virtual void Dispose(bool disposing) {
162+
if (disposed) return;
163163
// cleanup
164164
...
165165
disposed = true;
@@ -171,12 +171,12 @@ public class DisposableResourceHolder : IDisposable {
171171

172172
When doing so, it is important that you make the `Close` implementation identical to `Dispose` and consider implementing the `IDisposable.Dispose` method explicitly.
173173

174-
```
174+
```csharp
175175
public class Stream : IDisposable {
176-
IDisposable.Dispose(){
176+
IDisposable.Dispose() {
177177
Close();
178178
}
179-
public void Close(){
179+
public void Close() {
180180
Dispose(true);
181181
GC.SuppressFinalize(this);
182182
}
@@ -195,29 +195,29 @@ public class Stream : IDisposable {
195195

196196
The following code shows an example of a finalizable type:
197197

198-
```
198+
```csharp
199199
public class ComplexResourceHolder : IDisposable {
200200

201201
private IntPtr buffer; // unmanaged memory buffer
202202
private SafeHandle resource; // disposable handle to a resource
203203
204-
public ComplexResourceHolder(){
204+
public ComplexResourceHolder() {
205205
this.buffer = ... // allocates memory
206206
this.resource = ... // allocates the resource
207207
}
208208

209-
protected virtual void Dispose(bool disposing){
209+
protected virtual void Dispose(bool disposing) {
210210
ReleaseBuffer(buffer); // release unmanaged memory
211-
if (disposing){ // release other disposable objects
211+
if (disposing) { // release other disposable objects
212212
if (resource!= null) resource.Dispose();
213213
}
214214
}
215215

216-
~ ComplexResourceHolder(){
216+
~ComplexResourceHolder() {
217217
Dispose(false);
218218
}
219219

220-
public void Dispose(){
220+
public void Dispose() {
221221
Dispose(true);
222222
GC.SuppressFinalize(this);
223223
}
@@ -236,14 +236,14 @@ public class ComplexResourceHolder : IDisposable {
236236

237237
When implementing the finalizer, simply call `Dispose(false)` and place all resource cleanup logic inside the `Dispose(bool disposing)` method.
238238

239-
```
239+
```csharp
240240
public class ComplexResourceHolder : IDisposable {
241241

242-
~ ComplexResourceHolder(){
242+
~ComplexResourceHolder() {
243243
Dispose(false);
244244
}
245245

246-
protected virtual void Dispose(bool disposing){
246+
protected virtual void Dispose(bool disposing) {
247247
...
248248
}
249249
}

0 commit comments

Comments
 (0)