Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you want to initialize a dialog with a using statment?

I'm adapting the coding style guide from this source: http://www.csharpfriends.com/articles/getarticle.aspx?articleid=336

Under "5.2 Initialization", it recommends the following:

If you initialize a dialog try to use the using statement:

using (OpenFileDialog openFileDialog = new OpenFileDialog()) { }

What are the reasons for this style choice?

like image 928
Michael Allan Jackson Avatar asked Apr 01 '11 19:04

Michael Allan Jackson


1 Answers

Chances are you only need the dialog short-term for immediate input. So, with a using statement, you can free the resources after you've completed what you need from it (Dispose it).

Using is just syntactical sugar for calling the dispose method after use.

like image 107
Brad Christie Avatar answered Oct 19 '22 23:10

Brad Christie