Monday, August 4, 2014

Understanding IDisposable

I've been going over this issue recently and just want to make sure my understanding is correct. I found a good post on Stack Overflow here:


http://ift.tt/1AQojTy


The answer seems very informative. Actually, the recommended pattern here is the same pattern that Microsoft recommends through the code analyser. But, I still don't understand the answer to my question.


I understand that if there are unmanaged resources, I should create a finalizer so that the resources are eventually released, and they are released at a different point to the managed resources. However, that's an extreme situation. There are very, very few unmanaged resources in our system, so I'm still struggling to understand why we should implement this clunky pattern for the sake of something that might happen in 1 out of 300 classes.


Again, if I build a class, and do not add a finalizer, is one added automatically behind the scenes? If not, what's the point of calling GC.SuppressFinalizer?


Mike's answer was "the Dispose pattern is designed to deal with things like inheritance."


But, I don't really get it. What does inheritance have to do with this? If there is no finalizer on the class, then GC.SupressFinalizer will do nothing as my understanding goes.


No comments:

Post a Comment