using( FileStream fs = File.Open( "log.txt",
FileMode.Append,
FileAccess.Write,
FileShare.None ) ) {
Byte[] msg =
new UTF8Encoding(true).GetBytes("Doing Some" +
" More Stuff\n");
fs.Write( msg, 0, msg.Length );
}
}
static void Main() {
DoSomeStuff();
DoSomeMoreStuff();
}
}
As you can see, the code is much easier to follow, and the using statement takes care of having
to type all those explicit try/finally blocks. You probably won’t be surprised to notice that if you
look at the generated code in ILDASM, the compiler has generated the try/finally blocks in place
of the using statement. You can also nest using statements within their compound blocks, just as
you can nest try/finally blocks.
Even though the using statement solves the “ugly code” symptom and reduces ...