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

后端 未结 8 1153
陌清茗
陌清茗 2021-02-04 23:49

I was browsing a coworkers c# code today and found the following:

    using (MemoryStream data1 = new MemoryStream())
    using (MemoryStream data2 = new MemoryS         


        
8条回答
  •  甜味超标
    2021-02-05 00:15

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

提交回复
热议问题