Does each user-defined class require an IDisposable garbage collection interface

I'm not sure how custom class objects collect garbage. Do I need to implement the IDisposable interface for each class and call the dispose () method to free it?

+5
source share
7 answers

No. Every normal .NET managed object gets garbage collected when you stop referencing it. IDisposable means that you implement the Dispose () method, which should be called by the caller - it usually releases things that are not going to garbage. It also helps to have a deterministic place to release memory.

Check out the IDisposable template to make sure you do it right:

http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/09/21/10887.aspx

+7
source

No, each object is automatically located.

IDisposable , , . , , , Dispose.

+3

IDisposable , , . .NET .

IDisposable . Dispose .

: ClR # .

+1

. , . classObject = null. , classObject, .

IDisposable , using().

+1

, . Dispose() , , , . , , .

IDisposable, , fx using

using (SomeClassWithDisp object = new SomeClassWithDisp())
{
    //Use the object
}

.:

IDisposable #?

+1

The dispose () method you use is used to explicitly free unmanaged resources (files, streams, descriptors, etc.) to which your object contains a link.

The idea is that you release these resources as soon as possible by calling the dispose method. The dispose () method will not IMMEDIATELY start garbage collection on your object, but rather free up resources and let the garbage collector do its job when convenient.

+1
source

All Articles