Is there a better deterministic disposal pattern than nested “using”s?

前端 未结 10 1431
难免孤独
难免孤独 2021-02-18 18:23

In C#, if I want to deterministically clean up non-managed resources, I can use the \"using\" keyword. But for multiple dependent objects, this ends up nesting further and furt

10条回答
  •  被撕碎了的回忆
    2021-02-18 18:42

    You can put using statements together before the opening braces like so:

      using (StreamWriter w1 = File.CreateText("W1"))
      using (StreamWriter w2 = File.CreateText("W2"))
      {
          // code here
      }
    

    http://blogs.msdn.com/ericgu/archive/2004/08/05/209267.aspx

提交回复
热议问题