FileStream / StreamWriter in .NET Core 1.1 does not have a Close () method

I use .net core 1.1, earlier, when I was with a .net framework, I usually call Close() on FileStream or any stream after stream operations have completed, but the FileStream class in .net core 1.1 does not have a Close method, I found Dispose() , but I don’t know if it is equivalent. Does anyone want to tell me the correct way to properly close the new FileStream/StreamWriter class in the .net core?

+6
source share
3 answers

The implementation of IDisposable means that you can use the using statement, which implicitly calls the Dispose() method, thereby closing the stream.

Use

 using (StreamWriter sw = new StreamWriter(path)) { // your logic here } // here Dispose() is called implicitly and the stream is closed 
+5
source

Use using or create your own Dispose Pattern .

 using (StreamWriter sw = new StreamWriter(path) { } 
0
source

By running this problem, trying to keep the legacy code intact, another option is to write an extension method.

 public static class FileStreamExtension { public static void Close(this FileStream reader) { reader.Dispose(); } } 
0
source

Source: https://habr.com/ru/post/1012477/


All Articles