I am at a loss on how to handle a class that contains variables with Dispose/Finalize methods. I wish for this class to contain its own Dispose/Finalize methods that call upon the Dispose for each variable; however, both the C# documentation and all other answers/examples on StackOverflow are causing some confusion.
The primary confusion comes from the lack of clarification between what's a "managed" or "unmanaged" object. For example, the documentation here that explains how to implement Dispose simply uses placeholder comments that simply state "Free any unmanaged objects here." or "Free any other managed objects here."
Is a class variable with Dispose/Finalize fall under the category of managed or unmanaged? (Furthermore, should I be concerned about class variables that do NOT contain any sort of Dispose/Finalize implementations? Considering how there's two types of "management", does that mean those without "Dispose" still need to be disposed somehow?)
I.e., what would be the correct way to handle this class?
class BaseClass : IDisposable {
MyDisposeableObject disposeMe; // object with Dispose/Finalize
Dictionary<string,int> anotherObject; // just some arbitrary other object
bool disposed = false;
public BaseClass() {
disposeMe = new MyDisposeableObject();
anotherObject = new Dictionary<string,int>();
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (disposed)
return;
if (disposing) {
// Free any other managed objects here.
// Should I use "disposeMe.Dispose()" here?
}
// Free any unmanaged objects here.
// OR should I use "disposeMe.Dispose()" here?
// Also should I be doing anything with "anotherObject"?
disposed = true;
}
~BaseClass() {
Dispose(false);
}
}