Both classes for practicality sake are disposable.
I understand what a using block does. But I'm not sure of all of the ways it can or needs to be used.
For example is this correct?
using (MyClass myClass = new MyClass(params))
{
myClass.name = "Steve";
SecondClass myClassSecond = new SecondClass(params);
myClassSecond.name = "George";
myClassSecond.msg = "Hello Man in the Yellow Hat";
}
Are both classes above disposed of?
Or do I need both inside a using statement?
using (MyClass myClass = new MyClass(params))
{
myClass.name = "Steve";
using (SecondClass myClassSecond = new SecondClass(params))
{
myClassSecond.name = "George";
myClassSecond.msg = "Hello Man in the Yellow Hat";
}
}
Is the above correct, or is there a better way to use multiple using statements?
The simple answer is if the class has a dispose method, use using.
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx
If it does not, no need.
http://msdn.microsoft.com/en-us/library/system.random.aspx
I've always wished this was more in your face in the MSDN documentation. At the end of the day, it's the classes that implement IDisposable, but really what I want is some sort of giant icon on the page that in some way says. 'Pay attention dummy! You must manage this yourself!'
I like to implement
IDisposable
in the classes I create if they use lots of resources or if I create it as a wrapper for file handling or network resources.In class objects that I use, I think it is important to call with a
using
block if the object in question opens files, creates network connections, etc.Some developers will write a class that implements
IDisposable
just so they can put their code in the cleaner lookingusing
block, but I think this abuses the reason for creating something that is Disposable.That depends on what those classes are and how they dispose of any resources they use. The
using
statement is essentially the same thing as atry/finally
block (nocatch
) which disposes of its resources in thefinally
block (by calling.Dispose()
on the object in question).So, first of all, if the class in question doesn't implement
IDisposable
then it's a moot point, you can't use it in ausing
block in that case. If it does implementIDisposable
then chances are it does so for a reason and.Dispose()
should be called on that object when you're done with it. In those cases, it's prudent to wrap it in ausing
block to guarantee the disposal. (Well, "guarantee" in so much as thefinally
block will be executed. Which is usually the case, unless something much worse has happened.)using
statement allows the programmer to specify when objects that use resources should release them.IDisposable
interface.Dispose
method, which should release the object's resources.Here is the sample showing use of using statement:
A using statement can be exited either when:
using
statement is reached orAs you are saying that
., then your second approach is the appropriate one. that is:
Using blocks are handy when you are working with anything that implements
IDisposable
interface. MSDN:So, these is effectively an equivalent of
The primary advantages of
using
are: it automatically disposes the object upon leaving theusing
block, so (1) you won't forget to do it and (2) it does the cleanup in case of exception.Short rules:
using
block.IDisposable
in that class, instantiate a resource in constructor, cleanup inDispose
.The
using
block is purely a syntactically simple way of dealing with IDisposable objects. If you understand whatusing
does, then you should understand how it can and should be used.See this question for exactly what the
using
block is translated into: Uses of "using" in C#