James L. James L. - 17 days ago 7
C# Question

FileStream/StreamWriter in .NET Core 1.1 have no Close() method

I'm using .net core 1.1, previously when I was with .net framework, I usually call

Close()
on
FileStream
or any Stream after I finished the stream operations, but the
FileStream
class in .net core 1.1 doesn't have Close method, I found
Dispose()
but don't know if it's the equivalent. Anyone care to let me know the right way to correctly close with the new
FileStream/StreamWriter
class in .net core?

Answer

Implementing IDisposable means that you can use a using statement, which will implicitly call the Dispose() method, thus closing the stream.

Use

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