Using - what happens to my stream?

前端 未结 4 2084
甜味超标
甜味超标 2021-01-20 00:20

Maybe it is a trival question, but it\'s bothering me. And don\'t shout laud if it is a duplicate - I tried to search, but there are so many questions regarding using that i

4条回答
  •  梦毁少年i
    2021-01-20 00:46

    You have a constructor for StreamWriter (NET Framework 4.5 only) that allows specifying the leaveOpen boolean that defines whether your instance takes ownership of the underlying stream or not.

    If not specified (as in your example, or for previous versions of the framework), by default it's false, so closing (or disposing) the instance closes the underlying stream.

    Unless you set the leaveOpen parameter to true, the StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called.

    So there is no difference between both pieces of code you provided.

提交回复
热议问题