Do I need to use the "using" keyword in every object that implements IDisposable?

I am calling a third-party library where so many classes implement IDisposable.

Do I need to use a template for all of them?

+5
source share
4 answers

You have it , but it's a good practice.

It ensures that resources are cleaned correctly, whether or not exceptions occur.

IDisposable should only be implemented on classes that need to clean up resources, so ensuring that they do this is good practice.

, Dispose (- WCF ), .

, , .

+5

, , , .

, , .

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

+3

.

:

, , using. , using - , , using IDisposable obj:

var obj = // ...
try
{
    // ...
}
finally
{
    obj.Dispose();
}

, , .

Scope: IDisposable

, , using. , Dispose , , IDisposable .

: Finalize

, , . Dispose Finalize: Dispose , Finalize . Finalize Dispose, . , - . , , - . , , .

+3

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

Dispose? , . , , , , , try {...} finally {}, finally. , finally.

.

0

All Articles