Can the C# using statement be written without the curly braces?

你说的曾经没有我的故事 提交于 2019-12-03 11:27:25

问题


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?


回答1:


Yes, you can also put them in one using statement:

using (MemoryStream data1 = new MemoryStream(), 
                    data2 = new MemoryStream())
{
    // do stuff
}



回答2:


The same rules apply when you omit the curly braces in a for or an if statement.

Incidentally if you reflect into the compiled code, the compiler decompiler adds the braces.




回答3:


Exactly what he said. The code above is exactly the same as writing:

using (MemoryStream data1 = new MemoryStream()) 
{
    using (MemoryStream data2 = new MemoryStream())
    {
        // Lots of code
    }
}

You can omit the curly braces after an if/else/for/while/using/etc statement as long as there is only one command within the statement. Examples:

// 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();
}

// NOT Equivalent! (The first one ONLY wraps the p = "bob";!)
if (x == 5) 
p = "bob";
z.doStuff();

if (x == 5) {
   p = "bob";
   z.doStuff();
}



回答4:


This is viable but risky, because if somebody later decides they want to do something to data1 before other stuff happens to it, they might place it right after the data1's using, which would take it out of the entire scope of data2's using. This would likely break compilation but still is a risky and pointless syntax shortcut..




回答5:


Exactly what your colleague said, that is the equivalent of nesting the statements. The dispose for data2 would be called immediately before the dispose function for data1.




回答6:


If there is only one instruction which follow the statement, the bracets are not needed. It is just like with if statement.

if(true)
{
   Console.Writeline("hello")
}

means the same that

if(true)
   Console.Writeline("hello")



回答7:


As people have said: given there only being one line following a statement it will work without the curled braces. However, people are neglecting to show in their examples that that one line can be an if/using/for with it's own curled braces. Here is an example:

if(foo)
  if(bar)
  {
     doStuff();
  }


来源:https://stackoverflow.com/questions/3506619/can-the-c-sharp-using-statement-be-written-without-the-curly-braces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!