I have the following code:
RegistryKey installKey = Registry.LocalMachine.OpenSubKey(installKey);
I am running a static analysis tool on my code and it is giving me a defect saying that I am returning from the medthod without disposing installKey. I know you can call Dispose() on RegistryKey in .NET 4.0 or later but my code runs on .NET 3.5.
Does anybody know the best way to Dispose this RegistryKey and keep my static analysis tool happy?
You should wrap your code within a
usingblock, which will implicitly callDisposefor you . It’s unclear what static analysis tool you’re using, but hopefully it understandsusing:Note that you can also call
Disposeexplicitly, but you need to castRegistryKeytoIDisposablefirst: