7

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Michael Allan Jackson
  • 4,217
  • 3
  • 35
  • 45

7 Answers7

4

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.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
3

The OpenFileDialog implements the IDisposable interface. Given that dialogs typically have a lifetime of a particular method a using block ensures they will be properly disposed

using (OpenFileDialog dialog = new OpenFileDialog()) {
  // Some setup work 
  ...
  return dialog.ShowDialog();
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
3

It's not the greatest example, an OpenFileDialog already disposes all resources when the dialog closes. And it's a component, not a control. That's an implementation detail though. In general, calling ShowDialog() does not automatically dispose the form object. Unlike Show(). This is necessary so that you retrieve the dialog results without fearing an ObjectDisposedException. Now it is important that you dispose it yourself after doing so.

Which the using statement makes easy.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

You would do this for the same reason you'd use any using construct which is to ensure that the object gets disposed. OpenFileDialog implements IDisposable so it is up to the consumer to make sure that the instance gets disposed and the using construct ensures that .Dispose gets called on the object.

Dustin Hodges
  • 4,110
  • 3
  • 26
  • 41
0

The using statement in the c# language allows us to define an scope for an object lifetime. This statement obtains the resource specified, executes the statements and finally calls the Dispose() method of the object to clean it up.

HABJAN
  • 9,212
  • 3
  • 35
  • 59
0

Definition of using

using: Defines a scope, outside of which an object or objects will be disposed.

I guess the definition tells all the thing.

For more better understandings do read using Statement on msdn

what-is-the-c-using-block-and-why-should-i-use-it

Get More Info

Community
  • 1
  • 1
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
0

One key point not mentioned in the other answers is that all dialogs are always modal, which means that unlike regular forms that you might show, modal dialogs will block execution until the form goes away. That means by the time execution returns to you after showing the dialog, the dialog has already disappeared! Thus it is already time to dispose of it, which is why it is recommended that you use the usings statement.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195