Wednesday 8 May 2019

c# - The primary use of IDisposable interface











"IDisposable Interface" article tells:




The primary use of this interface is to release unmanaged resources





Why? Why only unmanaged?



Whole my life I thought its PRIMIRALY use is to release ANY resources: managed (connections to DBs, services proxies, etc) and unmanaged (if they are used in application).



P.S.



I believe there are already questions on this topic, but can't find them.


Answer



The underlying connections to db's are not managed, as are file handles and a number of other low-level o/s objects. They are unmanaged. Implementing an IDisposable interface implies that you are not just relying on the garbage collector to release those resources; but you are closing those resources using what ever low-level API that you have available.




Also, I think Eric Lippert's answer (2nd one down) to a similar question is a very good explanation on why you would use IDisposable.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...