Reputation: 8462
Why Do I have to implement Dispose from IDisposable, What if I just implement any method in my class that releases the unmanaged resource?
People I talk to pretend to know the reason just telling me I have to do so.
Honestly, I think this is the kind of question too-obvious-to-ask-although-Idon't-know between some developers
Upvotes: 3
Views: 1087
Reputation: 81105
The IDisposable
interface is unique in that its presence actually says less about a class than its absence. If a class implements IDisposable
, the class indicates that it might have asked some outside entity (which could be any kind of entity, anywhere in the universe) to do something on its behalf until further notice, to the possible detriment of other prospective users, and it might consequently have a responsibility to ensure that the outside entity gets notified when its services are no longer needed. For example, an object which encapsulates an input stream might have asked the underlying operating system for exclusive access to a file [which might possibly be on a different computer]; until either the object tells the OS that it is done with the file, or the application hosting the object terminates, nobody else anywhere in the universe will be able to access that file. The object thus has a responsibility to notify the OS when the file is no longer needed. Note that a consumer of an input stream wouldn't really care whether the stream has actually asked any outside entity to do anything on its behalf. Instead, as soon as the consumer of a stream knows it won't be needed any more, the consumer calls Dispose
on the stream, which can then perform whatever action, if any, is necessary to ensure that all entities who are still acting on its behalf (again, if any) are notified that they can stop doing so.
Note that IDisposable
doesn't say that a class will ever actually ask any outside entities to do anything on its behalf, nor that it actually has any cleanup responsibilities, nor that any actual harm would be caused by abandoning an instance without calling Dispose
. If a class does not implement IDisposable
, however, that provides a very strong indication that instances of the class have not assumed any responsibility to ensure outside resources get cleaned up. At minimum, it provides a strong indication that the object may be safely abandoned without leaving any necessary cleanup actions undone [some types like WeakReference
require cleanup but do not implement IDisposable
, instead relying upon finalization]
Consequently, I would posit that a good reason for classes to implement IDisposable
rather than using some other cleanup method is to avoid giving the false impression that cleanup isn't really required.
Upvotes: 2
Reputation: 124632
People I talk to pretend to know the reason just telling me I have too.
Because it establishes a common interface (method) of releasing native resources. When you properly implement IDisposable
I can now:
IDisposable
interface if I need to. using
statement instead of writing try/finally
all over the place. Dispose()
on an instance of your class (i.e., instances of classes which implement IDisposable
).Dispose()
as long as you are implementing your class correctly) if I make the mistake of not calling Dispose()
directly. Not saying that you should do this as it makes resource management non-deterministic, but it is a nice just in case feature.You don't have to implement IDisposable
, you just should when your type is managing native resoruces (resources which cannot be managed by the runtime).
Upvotes: 13
Reputation: 363
If you can make sure that your release function will be called under all circumstance, no matter what happens, exception, application crash etc etc. then I will say, you don't have to implement IDisposable interface.
but the truth is, If you are writing a class , or a component, you can't predict how your customer is going to use it. they might forget to call your release function. which will result your resource not release.
Implement IDisposable interface is a good practice
Upvotes: 0