I was browsing a coworkers c# code today and found the following:
using (MemoryStream data1 = new MemoryStream())
using (MemoryStream data2 = new MemoryStream())
{
// Lots of code..........
}
I had always seen the using
statement followed by a pair of curly braces that defined the scope of the object life. My coworker who wrote the code said that the curly braces for the data1
using
statement weren't needed and the code did the same thing as if they were present and nested the data2
using
statement. So, what happens when the curly braces are ommitted?
Yes, you can also put them in one using statement:
using (MemoryStream data1 = new MemoryStream(),
data2 = new MemoryStream())
{
// do stuff
}