Using - what happens to my stream?

前端 未结 4 2133
甜味超标
甜味超标 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条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-20 00:53

    Use {} always! It makes the intention of your code a lot better. Your code looks like:

    using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (StreamWriter writeFile = new StreamWriter(new    IsolatedStorageFileStream("example.txt", FileMode.Create, ISF)))
        {
            writeFile.WriteLine("Example");
        }
    }
    

    You can then see that the StreamWriter is executed in the context of the ISF. If I understand the ISF correctly, the ISF should not be closed when the Streamwriter closes the file. And you could open another File in the ISF Block.

提交回复
热议问题