Is there any difference between these two code samples and if not, why does using
exist?
StreamWriter writer;
try {
writer = new StreamWriter(...)
writer.blahblah();
} finally {
writer.Dispose();
}
vs:
using (Streamwriter writer = new Streamwriter(...)) {
writer.blahblah
}
I mean in the second example you really should be putting it in a try block anyway so adding the finally block really doesn't use much more effort. I get that the entire thing might be encompassed in a bigger try block but yeah, just seems superfluous to me.
foreach
, which is just a somewhat more concise version of calling the iterator methods and properties.Using
statement doesn't seem to be useful in par withTry-Finally
block. I also see this MSDN article saying the same with a WCF client.