Today, I wanted to perform an operation with a file so I came up with this code
class Test1
{
Test1()
{
using (var fileStream = new FileStream("c:\\test.txt", FileMode.Open))
{
//just use this filestream in a using Statement and release it after use.
}
}
}
But on code review, I was asked to implement IDisposable interface and Finalizer methods
class Test : IDisposable
{
Test()
{
//using some un managed resources like files or database connections.
}
~Test()
{
//since .NET garbage collector, does not call Dispose method, i call in Finalize method since .net garbage collector calls this
}
public void Dispose()
{
//release my files or database connections
}
}
But, my question is why should I ?
Although I cannot justify my methodology according to me, why should we use IDisposable when using statement can itself release resources)
Any specific advantages or am is missing something here?