I recently implemented a class like:
class TestClass : IDisposable
{
RegistryKey m_key;
public TestClass()
{
m_key = Registry.CurrentUser.OpenSubKey("Software", false);
}
public void Dispose()
{
// m_key.Dispose();
IDisposable disp = m_key;
disp.Dispose();
}
}
If I uncomment the direct call to Dispose, I get error CS0117 ("'Microsoft.Win32.RegistryKey' does not contain a definition for 'Dispose'"). Some Googling led me to this thread, where I learned what was going on, so I now understand the mechanics of it. The MSDN documentation suggests that the author would prefer that I call Close() instead of Dispose(), but doesn't explain why.
What is the purpose of this pattern (which I think I've seen it in the IO classes as well)? In light of the fact that this was an intentional decision by the class author, how bad is the code above (the call to Dispose through the IDisposable interface)? It can't be too bad - after all, it's what would happen in a using statement, right?
[edits: 1) changed title from "non-public" to "explicit" 2) removed the explicit implementation from my code, accidentally left in from experimentation]