In almost every case in C# when you have the choice of using braces you can substitute them with a single line of code.
Consider an if statement:
if (someBool)
DoSomething();
else
DoSomethingElse();
That is just as valid as the following:
if (someBool)
{
// do lots of things
}
else
DoSomethingElse();
This is probably almost universally true for any time you can use the {
and }
brackets.
The nice thing about this with the using
statement is that you can nest them like this:
using (var stream = new NetworkStream(...))
using (var sslStream = new SslStream(stream))
using (var reader = new StreamReader(sslStream))
{
reader.WriteLine(...);
}
This is equivalent to the following:
using (var stream = new NetworkStream(...))
{
using (var sslStream = new SslStream(stream))
{
using (var reader = new StreamReader(sslStream))
{
reader.WriteLine(...);
}
}
}
Although I think you'd agree it's much nicer looking.