Proper use of IDisposable interface in C#: how to avoid memory leaks
Hi everyone! I'm a beginner C# developer and I can't figure out the IDisposable interface. I've read the documentation, but I still have questions.\n\nHere's an example of my code. I'm working with files and databases:\n\n```csharp\npublic class FileProcessor\n{\n private StreamReader _reader;\n private SqlConnection _connection;\n\n public void ProcessFile(string path)\n {\n _reader = new StreamReader(path);\n _connection = new SqlConnection("...");\n \n // Read file and save to DB\n string line;\n while ((line = _reader.ReadLine()) != null)\n {\n // save to DB\n }\n \n // I don't close _reader and _connection - GC will do this, right?\n }\n}\n```\n\nI know that StreamReader and SqlConnection implement I
No answers yet.
Log in to leave an answer