Skip to content

Recommendations are confusing and conflicting #32837

@Robin-G

Description

@Robin-G

Hello,

the article recommends using finalizers in order to release resources. However, it - rightly - contains a note, that highlights that finalizers may not be called at all (for .NET 5 or later).

Let us consider a resource which uses lock files. The resource is obtained in the constructor (the lock file is created) and released - as recommended - with a finalizer (which deletes the lock file). Since in .NET 5 and later the finalizer may not be called at the termination of the program the lock file remains and the resource is never again released. The article only suggests using IDisposable if it is necessary to release the resource fast (which isn't the case in this example).

The recommendation probably should be amended that only resources which are released automatically at the termination of the program can use the finalizer to free them. Otherwise they might never be released.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions