Skip to content

Latest commit

 

History

History
47 lines (44 loc) · 931 Bytes

20181210.using.md

File metadata and controls

47 lines (44 loc) · 931 Bytes
  1. 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
    }
}
  1. 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();
}
  1. 假设语句后面只有一行,那么不使用花括号也可以。然而,人们忽略了,这一行可以是一个if/using/for与它自己的卷曲括号。下面是一个例子:
if(foo)
  if(bar)
  {
     doStuff();
  }