Reputation: 533
I have been working on some code for a while. And I had a question:
What it the difference between these two codes ?
using (FORM formExemple = new FORM ())
{
formExemple.ShowDialog();
}
and
FORM formExemple = new FORM ();
formExemple.ShowDialog();
Upvotes: 0
Views: 172
Reputation: 20054
The using-statement ...
using (Font font1 = new Font("Arial", 10.0f))
{
byte charset = font1.GdiCharSet;
}
... is a syntactic shortcut (syntactic sugar) for the following pattern:
{
Font font1 = new Font("Arial", 10.0f);
try
{
byte charset = font1.GdiCharSet;
}
finally
{
if (font1 != null)
((IDisposable)font1).Dispose();
}
}
(I took the the example from the link above)
... and therefore the difference between your two code-snippets the try
+ the call to IDisposable.Dispose()
in the finally
-block.
Upvotes: 2
Reputation: 8818
using
calls the object's Dispose()
method to clean up after itself when it's done. It usually handles things like closing open connections and/or freeing up memory. If you instantiate it otherwise, you have to do that manually. You can only use using
on objects that implement the IDisposable
interface, which ensures that a method Dispose()
exists for the object.
Upvotes: 8
Reputation: 245429
The using block is used to automatically dispose of an object that implements IDisposable
.
The first gets Dispose()
called at the end of the block. The using block will also ensure that the object is properly disposed of in the case of an Exception.
The second doesn't and needs to be handled by the developer when they're sure that they will no longer need the object.
Upvotes: 6
Reputation: 2458
using
is something you can use with objects that have implement the IDisposable interface. The reason to use using is once you are done with your form, you don't have to worry about any clean up, the Dispose method on Form will be called and everything will be cleaned up.
In your second example, Dispose is never called and the objects are still in memory and could be for awhile if the garbage collector doesn't recognize that it needs to clean it up.
Upvotes: 4
Reputation: 68
The using statement defines the scope for formExemple, the memory allocated for formExemple will be freed once outside the control of the using statement.
Upvotes: -1
Reputation: 1500785
As ever, consult the documentation - either the MSDN C# guide for using
statements or the C# specification (it's section 8.13 in the C# 5 specification).
The using
statement can only be used with types implementing IDisposable
, and it basically makes sure that whatever resource appears in the inital expression (the resource acquisition expression) is disposed at the end of the block, even if an exception is thrown. Your first code snippet is broadly equivalent to:
FORM formExemple = new FORM(); // I hope you don't *really* use these names
try
{
formExemple.ShowDialog();
}
finally
{
formExemple.Dispose();
}
There's a little bit more to it than that, mind you:
using
statement itselfusing
statement (the original value is still disposed)Basically, it makes it easier to clean-up resources which require timely disposal - network streams, file handles, database connections, graphics objects etc.
Upvotes: 5