Skip to content

Gendarme.Rules.BadPractice.OnlyUseDisposeForIDisposableTypesRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

OnlyUseDisposeForIDisposableTypesRule

Assembly: Gendarme.Rules.BadPractice
Version: git

Description

To avoid confusing developers methods named Dispose should be reserved for types that implement IDisposable.

Examples

Bad example:

internal sealed class Worker
{
    // This class uses one or more temporary files to do its work.
    private List<string> files = new List<string> ();
    // This is confusing: developers will think they can do things
    // like use the instance with a using statement.
    public void Dispose ()
    {
        foreach (string path in files) {
            File.Delete (path);
        }
        files.Clear ();
    }
}

Good example:

internal sealed class Worker
{
    // This class uses one or more temporary files to do its work.
    private List<string> files = new List<string> ();
    public void Reset ()
    {
        foreach (string path in files) {
            File.Delete (path);
        }
        files.Clear ();
    }
}

Notes

  • This rule is available since Gendarme 2.6

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally