-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
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.
- ID: a96eaef8-dcc9-4659-3f4e-be5a1c4c6b50
- Version Independent ID: d3da7981-542d-e906-5988-b80e403fa651
- Content: Finalizers - C# Programming Guide
- Content Source: docs/csharp/programming-guide/classes-and-structs/finalizers.md
- Product: dotnet-csharp
- Technology: csharp-fundamentals
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn