Nesting 'IDisposable's in a single 'using' statement

后端 未结 3 1608
星月不相逢
星月不相逢 2021-01-18 10:48

Quick question about using nested disposables in a single \'using\' statement: Should I write out each disposable\'s using statement, or can I nest them into one? Example:

3条回答
  •  花落未央
    2021-01-18 11:06

    if when the block is done executing, the unnamed FileStream from "myFile.txt" gets cleaned up because it's in the using statement with the GZipStream

    Even when both constructors succeed, it still depends on the design of the 'owning' class. A StreamReader will close its BaseStream but many other classes won't.

    You don't want your code to depend on such murky and liable to change details.

提交回复
热议问题