c# - Is it necessary to have a using block if you explicitly dispose of a form? -


i've been getting win32exception infamous error: error creating window handle. until recently, realized happening on line tries call environment.exit(0) subsequently got rid of, , let form close itself.

so question is, whenever know i'm not going need form anymore, call base.close() , base.dispose(true). if that, necessary put form inside using block?

it not necessary. however, using block handy , fail-safe programming construct. ie. common forget adding call dispose. when choose 'using' block never need worry disposing object. so, better practice using 'using' block disposable objects.

you didn't mention placed call dispose method. should in block explained below.

disposableclass disposableobj = null; try {     disposableobj = new disposableclass();     ....     .... } {     if( disposableobj != null)     {        disposableobj.dispose();     } } 

the same above code can simplified as

using(disposableclass disposableobj = new disposableclass()) {   .....   ..... } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -