Skip to content

Commit 5d48f3c

Browse files
authored
Merge pull request #2504 from gewarren/ca-713
Replace {0} in code analysis IDisposable topic
2 parents 29e8364 + 25781e1 commit 5d48f3c

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

docs/code-quality/ca1063-implement-idisposable-correctly.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ ms.assetid: 12afb1ea-3a17-4a3f-a1f0-fcdb853e2359
1414
author: gewarren
1515
ms.author: gewarren
1616
manager: douge
17+
dev_langs:
18+
- "CSharp"
1719
ms.workload:
1820
- "multiple"
1921
---
@@ -28,51 +30,51 @@ ms.workload:
2830

2931
## Cause
3032

31-
`IDisposable` is not implemented correctly. Some reasons for this problem are listed here:
33+
The <xref:System.IDisposable?displayProperty=nameWithType> interface is not implemented correctly. Possible reasons for this include:
3234

33-
- IDisposable is re-implemented in the class.
35+
- <xref:System.IDisposable> is reimplemented in the class.
3436

35-
- Finalize is re-overridden.
37+
- Finalize is reoverridden.
3638

37-
- Dispose is overridden.
39+
- Dispose() is overridden.
3840

39-
- Dispose() is not public, sealed, or named Dispose.
41+
- The Dispose() method is not public, [sealed](/dotnet/csharp/language-reference/keywords/sealed), or named **Dispose**.
4042

4143
- Dispose(bool) is not protected, virtual, or unsealed.
4244

4345
- In unsealed types, Dispose() must call Dispose(true).
4446

45-
- For unsealed types, the Finalize implementation does not call either or both Dispose(bool) or the case class finalizer.
47+
- For unsealed types, the Finalize implementation does not call either or both Dispose(bool) or the base class finalizer.
4648

47-
Violation of any one of these patterns will trigger this warning.
49+
Violation of any one of these patterns triggers warning CA1063.
4850

49-
Every unsealed type that declares and implements the IDisposable interface must provide its own protected virtual void Dispose(bool) method. Dispose() should call Dipose(true) and Finalize should call Dispose(false). If you are creating an unsealed type that declares and implements the IDisposable interface, you must define Dispose(bool) and call it. For more information, see [Cleaning up unmanaged resources](/dotnet/standard/garbage-collection/unmanaged) in the [.NET Framework design guidelines](/dotnet/standard/design-guidelines/index).
51+
Every unsealed type that declares and implements the <xref:System.IDisposable> interface must provide its own protected virtual void Dispose(bool) method. Dispose() should call Dipose(true), and the finalizer should call Dispose(false). If you create an unsealed type that declares and implements the <xref:System.IDisposable> interface, you must define Dispose(bool) and call it. For more information, see [Clean up unmanaged resources (.NET guide)](/dotnet/standard/garbage-collection/unmanaged) and [Dispose pattern](/dotnet/standard/design-guidelines/dispose-pattern).
5052

5153
## Rule description
5254

53-
All IDisposable types should implement the Dispose pattern correctly.
55+
All <xref:System.IDisposable> types should implement the [Dispose pattern](/dotnet/standard/design-guidelines/dispose-pattern) correctly.
5456

5557
## How to fix violations
5658

57-
Examine your code and determine which of the following resolutions will fix this violation.
59+
Examine your code and determine which of the following resolutions will fix this violation:
5860

59-
- Remove IDisposable from the list of interfaces that are implemented by {0} and override the base class Dispose implementation instead.
61+
- Remove <xref:System.IDisposable> from the list of interfaces that are implemented by your type, and override the base class Dispose implementation instead.
6062

61-
- Remove the finalizer from type {0}, override Dispose(bool disposing), and put the finalization logic in the code path where 'disposing' is false.
63+
- Remove the finalizer from your type, override Dispose(bool disposing), and put the finalization logic in the code path where 'disposing' is false.
6264

63-
- Remove {0}, override Dispose(bool disposing), and put the dispose logic in the code path where 'disposing' is true.
65+
- Override Dispose(bool disposing), and put the dispose logic in the code path where 'disposing' is true.
6466

65-
- Ensure that {0} is declared as public and sealed.
67+
- Make sure that Dispose() is declared as public and [sealed](/dotnet/csharp/language-reference/keywords/sealed).
6668

67-
- Rename {0} to 'Dispose' and make sure that it is declared as public and sealed.
69+
- Rename your dispose method to **Dispose** and make sure that it's declared as public and [sealed](/dotnet/csharp/language-reference/keywords/sealed).
6870

69-
- Make sure that {0} is declared as protected, virtual, and unsealed.
71+
- Make sure that Dispose(bool) is declared as protected, virtual, and unsealed.
7072

71-
- Modify {0} so that it calls Dispose(true), then calls GC.SuppressFinalize on the current object instance ('this' or 'Me' in [!INCLUDE[vbprvb](../code-quality/includes/vbprvb_md.md)]), and then returns.
73+
- Modify Dispose() so that it calls Dispose(true), then calls <xref:System.GC.SuppressFinalize%2A> on the current object instance (`this`, or `Me` in Visual Basic), and then returns.
7274

73-
- Modify {0} so that it calls Dispose(false) and then returns.
75+
- Modify your finalizer so that it calls Dispose(false) and then returns.
7476

75-
- If you are creating an unsealed type that declares and implements the IDisposable interface, make sure that the implementation of IDisposable follows the pattern that is described earlier in this section.
77+
- If you create an unsealed type that declares and implements the <xref:System.IDisposable> interface, make sure that the implementation of <xref:System.IDisposable> follows the pattern that is described earlier in this section.
7678

7779
## When to suppress warnings
7880

@@ -96,7 +98,7 @@ public class Resource : IDisposable
9698
}
9799

98100
// NOTE: Leave out the finalizer altogether if this class doesn't
99-
// own unmanaged resources itself, but leave the other methods
101+
// own unmanaged resources, but leave the other methods
100102
// exactly as they are.
101103
~Resource()
102104
{
@@ -124,4 +126,9 @@ public class Resource : IDisposable
124126
}
125127
}
126128
}
127-
```
129+
```
130+
131+
## See also
132+
133+
- [Dispose pattern (framework design guidelines)](/dotnet/standard/design-guidelines/dispose-pattern)
134+
- [Clean up unmanaged resources (.NET guide)](/dotnet/standard/garbage-collection/unmanaged)

0 commit comments

Comments
 (0)