6 Answers
6

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.

Correct. Just remember that Dispose() does nothing magical. You can inherit from IDisposable, and create your Dispose() method, but unless you actually manually code something in it, it's really just calling an empty method. THe nice thing about using is it does call the dispose() and it limits the scope.
–
Ryan TernierSep 26 '12 at 20:45

@RyanTernier I almost put in a disclaimer that Dispose wasn't magic, but I decided against it because I figured that the last sentence would imply that Dispose() is just part of an interface
–
Phillip SchmidtSep 26 '12 at 20:49

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.

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:

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.

Not necessarily. Using will call the object's Dispose() method, which could do whatever it wants.
–
Phillip SchmidtSep 26 '12 at 20:27

The memory will be freed whenever the garbage collector feels like freeing it. It will be eligible for collection once it is out of scope. That could be after the end of the using block (but not if you still hold onto a reference to it, which you can do). In any case, this isn't the purpose of a using, it's merely a side effect. You can use a code block without a using at all: { Form f = new Form();}
–
ServySep 26 '12 at 20:29

Well unless you override the Dispose() method that is what is happening by default right?
–
AnthonyFGSep 26 '12 at 20:33

does the using make it eligible for collection when out of scope?
–
AnthonyFGSep 26 '12 at 20:35

nobody is saying anything about scope though, formexemple goes out of scope after that block
–
AnthonyFGSep 26 '12 at 20:41