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

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

    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")
    

提交回复
热议问题