registry - C#: Is there a clean pattern for finding the right object combined with using? -


i clean/compact pattern exception safe , dispose ruleskey, if has thrown. not appear possible using (unless maybe 4 usings seems verbose , opening resources might not need opened). gets me direct/easy in c++. there solution?

{     registrykey ruleskey = null;     ruleskey = ruleskey ?? registry.localmachine.opensubkey("software\\wow6432node\\company\\internal\\product");     ruleskey = ruleskey ?? registry.localmachine.opensubkey("software\\company\\company\\internal\\product");     ruleskey = ruleskey ?? registry.localmachine.opensubkey("software\\wow6432node\\company\\product");     ruleskey = ruleskey ?? registry.localmachine.opensubkey("software\\company\\product");      // code using ruleskey, might throw      ruleskey.close(); } 

you use

using (registrykey ruleskey = registry.localmachine.opensubkey("software\\wow6432node\\company\\internal\\product")                                 ?? registry.localmachine.opensubkey("software\\company\\company\\internal\\product")                                 ?? registry.localmachine.opensubkey("software\\wow6432node\\company\\product")                                 ?? registry.localmachine.opensubkey("software\\company\\product")) {     //use ruleskey here } 

since

the using statement ensures dispose called if exception occurs while calling methods on object. can achieve same result putting object inside try block , calling dispose in block; in fact, how using statement translated compiler. msdn


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -