- using
using (MemoryStream data1 = new MemoryStream())
using (MemoryStream data2 = new MemoryStream())
{
// Lots of code..........
}
//同
using (MemoryStream data1 = new MemoryStream(),
data2 = new MemoryStream())
{
// do stuff
}
//同
using (MemoryStream data1 = new MemoryStream())
{
using (MemoryStream data2 = new MemoryStream())
{
// Lots of code
}
}
- if/using/for,只有一行语句时,可以省略花括号,如
// Equivalent!
if (x==6)
str = "x is 6";
if(x == 6) {
str = "x is 6";
}
// Equivalent!
for (int x = 0; x < 10; ++x) z.doStuff();
for (int x = 0; x < 10; ++x) {
z.doStuff();
}
- 假设语句后面只有一行,那么不使用花括号也可以。然而,人们忽略了,这一行可以是一个if/using/for与它自己的卷曲括号。下面是一个例子:
if(foo)
if(bar)
{
doStuff();
}