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

后端 未结 8 1044
陌清茗
陌清茗 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:32

    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..

提交回复
热议问题