Correct usage of IDisposable interface in C#: how to avoid memory leaks?
Hello everyone! I'm a beginner C# developer and I can't figure out the IDisposable interface. I've read the documentation, but in practice questions still arise.
I have a class that works with files and network connections. I implemented IDisposable, but I'm not sure I'm doing it correctly. Here's an example of my code:
public class FileProcessor : IDisposable
{
private FileStream _fileStream;
private NetworkStream _networkStream;
private bool _disposed = false;
public void ProcessFile(string path)
{
_fileStream = new FileStream(path, FileMode.Open);
// some processing
}
public void Dispose()
{
if (_fileStream != null)
_fileStream.Dispose();
if (_networkStream != null) No answers yet.
Log in to leave an answer