简体   繁体   中英

What is the best way to handle destruction of objects in C#

I have a Class Library project which has a class that creates a file during object construction. The file has to be deleted when the object is done. I have implemented IDisposable and written the Delete code in Dispose(). The issue is, I still see the file. I remember reading somewhere that Dispose is not guaranteed to be called. What would be the best way to accomplish this ?

Best practice in .NET is to use the using block. I would expect that you cannot guarantee that your code will always be GC'd if someone writes code that orphans it; however

Using Statement

Provides a convenient syntax that ensures the correct use of IDisposable objects.

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

using(YourDisposable iCanDispose = new YourDisposable())
{

  // work with iCanDispose
}

Will ensure that the objects dispose method is called once it has left scope.

The using keyword is your friend in this case. Anything that implements IDisposable can use it, and it gives you a better mechanism to ensure your objects are disposed.

It's usage looks something like this:

using (SomeIDisposableObject someThing = new SomeIDisposableObject())
{
   // Do some work here and don't sweat to much because once I fall out-of-scope
   // I will be disposed of.
}    

You can also nest them, like so:

using (SomeIDisposableObject someThing = new SomeIDisposableObject())
{
   // I am next up for being disposed of.
   using (SomeOtherIDisposableObject someOtherThing = new SomeOtherIDisposableObject())
   {
      // I will get disposed of first since I am nested.
   }
}

You can also stack them:

using (SomeIDisposableObject someThing = new SomeIDisposableObject())
{
   // I will be disposed of.
}

using (SomeOtherIDisposableObject someOtherThing = new SomeOtherIDisposableObject())
{
   // I will also be disposed of.
}

you can call Dispose() function on any object (which implements the IDisposable Interface) inside the finally block to guarantee the object distruction.

using block can be used to simplify the above solution without creating try and finally blocks. so any object which needs to use using() block should implement IDisposable Interface so that object will be Disposed immediately ,as soon as it comes out of using block.

using syntax:

using(object declaration and initialization)
{
//statements
}

the using block is similar to following:

try
{
//object declaration and initialization
}
finally
{
//Call Object's Dispose() 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM