All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S3966.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

Why is this an issue?

Disposing an object twice in the same method, either with the using keyword or by calling Dispose directly, is confusing and error-prone. For example, another developer might try to use an already-disposed object, or there can be runtime errors for specific paths in the code.

In addition, even if the documentation explicitly states that calling the Dispose method multiple times should not throw an exception, some implementations still do it. Thus it is safer to not dispose of an object twice when possible.

How to fix it

Code examples

Noncompliant code example

var foo = new Disposable();
foo.Dispose();
foo.Dispose(); // Noncompliant
using (var bar = new Disposable()) // Noncompliant
{
    bar.Dispose();
}

Compliant solution

var foo = new Disposable();
foo.Dispose();
using (var bar = new Disposable()) // Compliant
{

}

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy