Reputation:
HOW do i know when i need to dispose of something? Someone just mention i had several objects in my code that i need to dispose of. I had no idea i needed to dispose anything (this is my first week with C#). How do i know when i need to dispose an object? i was using http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx and i do not see any mention of dispose on the page or seen it mention in any other objs i was told i to dispose (by someone on SO).
I know i need to when something inherits IDisposable but HOW do i KNOW when it does inherit it?
Upvotes: 4
Views: 2263
Reputation: 81105
"Will the last person to leave the room please turn out the lights?"
An object which implements IDisposable holds the information and impetus necessary to do some "clean-up" operations that should happen "sometime", but which can't happen while the object is still in use. If the object is totally abandoned, those clean-up operations won't happen. The system includes a custodian, with which objects can register when they are created; if an object is abandoned by absolutely everyone but the custodian, the custodian can ask the object to perform its cleanup actions before the custodian too abandons it. Note that for a variety of reasons, the custodian isn't 100% effective at handling abandoned objects. It is thus very desirable that, whenever possible, the last entity to hold a useful reference to an object dispose of it before abandoning the reference.
Upvotes: 0
Reputation:
An easy way would be to type obj.disp and see if intellisense has a dispose method.
Upvotes: 3
Reputation: 30047
I know i need to when something inherits IDisposable but HOW do i KNOW when it does inherit it?
Assuming you're using Visual Studio. I usually right click on the type, then "Go To Definition". If I see that it, or any of its super classes, implement IDisposable, I make sure I call Dispose on it. This is typically done by wrapping it in a using block as others have mentioned.
Upvotes: 0
Reputation: 700182
The class implements the interface IDisposable
, that means that it has a Dispose
method.
Not every class that implements IDisposable requires you to call Dispose
, but most of them do. If you see that the class implements IDisposable
(or has a Dispose
method because it inherits the interface from a base class), you have two choises:
Dig deep in the documentation to find out why the class implements IDisposable
, and if you really need to call Dispose
.
Just call Dispose
.
Either method is safe. If the Dispose
method doesn't do anything, the call will be very quick. You can even call Dispose
more than once without harm.
Even better then just calling the Dispose
method is to use a using
block:
using (FileStream s = File.OpenRead(path)) {
...
}
At the end bracket of the block the Dispose
method is called automatically. The using
block is implemented as a try...finally
, so the Dispose
method is guaranteed to be called even if an exception occurs in the block.
Upvotes: 2
Reputation: 19911
You should dispose of any object that implements the IDisposable interface.
public abstract class HashAlgorithm : ICryptoTransform,
IDisposable
Anything that has unmanaged resources (DB connections for example) should implement the IDisposable interface.
There are a couple of good reasons for this:
Upvotes: 0
Reputation: 29495
If an class implements IDisposable you should dispose of intances of that class. If it doesn't you don't. In this case HashAlgorithm derives from ICryptoTransform which derives from IDisposable. This means all instance of classes descending from HashAlgorithm must be disposed.
Upvotes: 0
Reputation: 36027
You should dispose anything that implements IDisposable. Just wrap it on an using:
using(var some = new Something())
{
//use normally
}
Upvotes: 5