Any sense to set obj = null(Nothing) in Dispose()?

Posted by serhio on Stack Overflow See other posts from Stack Overflow or by serhio
Published on 2010-02-17T11:06:08Z Indexed on 2010/03/12 1:47 UTC
Read the original article Hit count: 277

Filed under:
|
|
|
|

Is there any sense to set custom object to null(Nothing in VB.NET) in the Dispose() method? Could this prevent memory leaks or it's useless?!

Let's consider two examples:

public class Foo : IDisposable
{
    private Bar bar; // standard custom .NET object

    public Foo(Bar bar) {
        this.bar = bar;
    }
    public void Dispose() {
        bar = null; // any sense?
    }
}

public class Foo : RichTextBox
{
    // this could be also: GDI+, TCP socket, SQl Connection, other "heavy" object
    private Bitmap backImage; 

    public Foo(Bitmap backImage) {
        this.backImage = backImage;
    }

    protected override void Dispose(bool disposing) {
        if (disposing) {
            backImage = null;  // any sense?
        }
    }
}

© Stack Overflow or respective owner

Related posts about .NET

Related posts about dispose