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
- A method that is an implementation of <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> does not call <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
34
+
Violations of this rule can be caused by:
35
+
36
+
- A method that is an implementation of <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> and doesn't call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
37
+
38
+
- A method that is not an implementation of <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> and calls <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
39
+
40
+
- A method that calls <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> and passes something other than [this (C#)](/dotnet/csharp/language-reference/keywords/this) or [Me (Visual Basic)](/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me).
41
+
42
+
## Rule description
43
+
44
+
The <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method lets users release resources at any time before the object becoming available for garbage collection. If the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method is called, it frees resources of the object. This makes finalization unnecessary. <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> should call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> so the garbage collector doesn't call the finalizer of the object.
45
+
46
+
To prevent derived types with finalizers from having to reimplement <xref:System.IDisposable> and to call it, unsealed types without finalizers should still call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
47
+
48
+
## How to fix violations
31
49
32
-
- A method that is not an implementation of <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> calls <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
50
+
To fix a violation of this rule:
33
51
34
-
- A method calls <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> and passes something other than this (Me in Visual Basic).
52
+
-If the method is an implementation of <xref:System.IDisposable.Dispose%2A>, add a call to <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
35
53
36
-
## Rule Description
37
-
The <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> method lets users release resources at any time before the object becoming available for garbage collection. If the <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> method is called, it frees resources of the object. This makes finalization unnecessary. <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> should call <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> so the garbage collector does not call the finalizer of the object.
54
+
- If the method is not an implementation of <xref:System.IDisposable.Dispose%2A>, either remove the call to <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> or move it to the type's <xref:System.IDisposable.Dispose%2A> implementation.
38
55
39
-
To prevent derived types with finalizers from having to re-implement <xref:System.IDisposable> and to call it, unsealed types without finalizers should still call <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
56
+
- Change all calls to <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>to pass [this (C#)](/dotnet/csharp/language-reference/keywords/this) or [Me (Visual Basic)](/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me).
40
57
41
-
## How to Fix Violations
42
-
To fix a violation of this rule:
58
+
## When to suppress warnings
43
59
44
-
If the method is an implementation of <xref:System.IDisposable.Dispose%2A>, add a call to <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
60
+
Only suppress a warning from this rule if you are deliberately using <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> to control the lifetime of other objects. Don't suppress a warning from this rule if an implementation of <xref:System.IDisposable.Dispose%2A> doesn't call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>. In this situation, failing to suppress finalization degrades performance and provides no benefits.
45
61
46
-
If the method is not an implementation of <xref:System.IDisposable.Dispose%2A>, either remove the call to <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> or move it to the type's <xref:System.IDisposable.Dispose%2A> implementation.
62
+
## Example that violates CA1816
47
63
48
-
Change all calls to <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> to pass this (Me in Visual Basic).
64
+
This code shows a method that calls <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>, but doesn't pass [this (C#)](/dotnet/csharp/language-reference/keywords/this) or [Me (Visual Basic)](/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me). As a result, this code violates rule CA1816.
49
65
50
-
## When to Suppress Warnings
51
-
Only suppress a warning from this rule if you are deliberating using <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> to control the lifetime of other objects. Do not suppress a warning from this rule if an implementation of <xref:System.IDisposable.Dispose%2A> does not call<xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>. In this situation, failing to suppress finalization degrades performance and provide no benefits.
This example shows a method that correctly calls <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> by passing [this (C#)](/dotnet/csharp/language-reference/keywords/this) or [Me (Visual Basic)](/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me).
58
72
59
-
## Example
60
-
The following example shows a method that correctly calls <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
0 commit comments