Dispose method on RegistryKey class

M

Mark Pitman

If you look up the RegistryKey.IDisposable.Dispose Method in the MSDN
documentation (http://tinyurl.com/4rxra) it states the following:

"This member supports the .NET Framework infrastructure and is not
intended to be used directly from your code."

However, if you go to the overview of the RegistryKey class
(http://tinyurl.com/3q9ge) and look at the C# sample, they place the
RegistryKey instantiation inside of a using statement, which will end
up calling Dispose() on the RegistryKey object. So which is it? Are we
supposed to be calling Dispose() on this class or not?
 
J

Jon Skeet [C# MVP]

Mark Pitman said:
If you look up the RegistryKey.IDisposable.Dispose Method in the MSDN
documentation (http://tinyurl.com/4rxra) it states the following:

"This member supports the .NET Framework infrastructure and is not
intended to be used directly from your code."

However, if you go to the overview of the RegistryKey class
(http://tinyurl.com/3q9ge) and look at the C# sample, they place the
RegistryKey instantiation inside of a using statement, which will end
up calling Dispose() on the RegistryKey object. So which is it? Are we
supposed to be calling Dispose() on this class or not?

I'd go with calling Dispose. There are various places where the docs
say things like the above, and I don't think it's a good idea to go by
them, personally. It's fairly obvious what the regisry key's unmanaged
resources are, and I don't think it's a good idea not to clean them up.
 
R

Robert Jordan

Hi Mark,
If you look up the RegistryKey.IDisposable.Dispose Method in the MSDN
documentation (http://tinyurl.com/4rxra) it states the following:

"This member supports the .NET Framework infrastructure and is not
intended to be used directly from your code."

However, if you go to the overview of the RegistryKey class
(http://tinyurl.com/3q9ge) and look at the C# sample, they place the
RegistryKey instantiation inside of a using statement, which will end
up calling Dispose() on the RegistryKey object. So which is it? Are we
supposed to be calling Dispose() on this class or not?

RegistryKey.IDisposable.Dispose is a direct interface
implementation and it's only accessible when called as

((IDisposable)key).Dispose();

that's exaclty what happens at the and of an using-block.

If you don't use "using", then you should Close() the key.

bye
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top