Dispose pattern (ARCH1102)
Ce contenu n’est pas encore disponible dans votre langue.
Key dotnet.resource.dispose-pattern · Severity warning · Category resource
If a class holds a disposable field, it is responsible for releasing that resource by implementing IDisposable itself. Otherwise the resource leaks.
Incorrect
Section titled “Incorrect”public class ReportGenerator{ private readonly SqlConnection _connection = new(Config.Conn);}Correct
Section titled “Correct”public class ReportGenerator : IDisposable{ private readonly SqlConnection _connection = new(Config.Conn);
public void Dispose() => _connection.Dispose();}Implement IDisposable on the class and release all owned disposable fields inside Dispose.