Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple destructor issue (IDisposable interface)

Tags:

c#

I am a beginner in C#, I don't know why this isn't working, I just want to set this object to null by calling Dispose() method.

Why this is not possible?

class MyClass:IDisposable
{

    public void Dispose()
    {
        this = null;
    }

}
like image 730
Jorge Tovar Avatar asked Dec 04 '22 10:12

Jorge Tovar


1 Answers

The purpose of the Dispose method isn't to clean up that class, but to clean up the disposable dependencies that the class is holding on to so that it can be disposed of normally by the garbage collector.

I'd suggest reading up more on the Dispose pattern and how to implement it in C#.

A bit of pedantry: The Dispose method is not a destructor, nor is it a finalizer.

like image 158
Daniel Mann Avatar answered Dec 17 '22 07:12

Daniel Mann