dotnet / dotnet/dotnet-api-docs

IDisposable Interface – finalizer provision in dervided classes in question

Open
#7,148 3 comments 0 reactions 0 assignees View on GitHub
area-System.Runtime Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

From [IDisposable Interface|Examples](https://docs.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-5.0#examples)

```csharp
// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
```

From [IDisposable and the inheritance hierarchy](https://docs.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-5.0#idisposable-and-the-inheritance-hierarchy)

> Subclasses should implement the disposable pattern as follows:
• They must override Dispose(Boolean) and call the base class Dispose(Boolean) implementation.
• They can provide a finalizer if needed. The finalizer must call Dispose(false).

Obviously these 2 statemens about finalizers (destructors) in derived classes are in contraposition.

----

Also this page does consistently uses either _finalizer_ or _destructor_ term. Correct term is [_finalizer_](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/destructors). (Also last segment of link to Finalizers page uses _destructors_.)

> Finalizers (historically referred to as destructors) are used to perform any necessary final clean-up when a class instance is being collected by the garbage collector.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.