tags:

views:

135

answers:

4

I am looking at some C# code written by someone else. Whenever a form is instantiated and then shown, the following is done. Is this correct? Why would you use "using" in this context?

MyForm f;
using (f = new MyForm())
{
    f.ShowDialog();
}

Additional question:

Could the following code be substituted?

using (MyForm f = new MyForm())
{
    f.ShowDialog();
}
+3  A: 

This restricts the resources held by the MyForm object f to the using block. Its Dispose method will be called when the block is exited and it is guaranteed to be "disposed" of at that time. Therefore any resources it holds will get deterministically cleaned up. Also, f cannot be modified to refer to another object within the using block. For more details, see the info about using in MSDN:


using in the C# Reference

Michael Goldshteyn
The using block does not restrict lifetime of an object. It provides a deterministic time in which to call Dispose. The object is still very much alive after the Dispose call.
JaredPar
Presumedly, the Dispose function will clean up the resources held in the object, rendering it useless, but your point is well taken and I will correct my answer.
Michael Goldshteyn
The .NET GC definitely does not guarantee garbage collection with such determinism. http://msdn.microsoft.com/en-us/library/system.idisposable.dispose(v=VS.80).aspx
Paul Sasik
Similarly, the GC won't call the Dispose method. The GC could put an object on the finalisation queue, but making sure Dispose is called will prevent the GC from doing so (presuming that Dispose suppresses finalisation, which it should in any finalisable class).
Jon Hanna
+3  A: 

Perhaps. If MyForm implements IDisposable, this will ensure that the Dispose method is called if an exception is thrown in the call to ShowDialog.

Otherwise, the using is not necessary unless you want to force disposal immediately

RyanHennig
+1: I can see this used well for Winforms + MVP. I dont know if code after show dialog will make the form dispose or even be executed
Perpetualcoder
+11  A: 

A Form in WinForms implements the IDisposable pattern (it inherits IDisposable from Component. The original author is correctly ensuring that the value will be disposed by means of the using statement.

JaredPar
You should explain that a call to ShowDialog will never dispose the form when it is closed. Read the 2nd paragraph of the Remark section at http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
Pierre-Alain Vigeant
If the form is going out of scope soon then is there a need to dispose of the form?
Craig Johnston
@Craig yes. When the form goes out of scope (assuming there are no other references) it becomes eligible for collection which will likely occur at some later point. Only at that later point will the form be able to clean up resources. The `using` block allows for the form to clean up resources as soon as it's done with them.
JaredPar
A: 

Yes, this is a 'correct' usage of IDisposable. Perhaps the author of MyForm had some large object (say a large MemoryStream) or file resource (e.g. an open FileStream) that it opened and wanted to make sure was released ASAP. Calling the MyForm ctor inside a using statement would be helpful in this case.

Question 2:

in C# 3.0+, you can use the shorter (and just as clear):

using (var f = new MyForm())

Jay