Dispose method on RegistryKey class

  • Thread starter Thread starter Mark Pitman
  • Start date Start date
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?
 
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.
 
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
 
Back
Top