Purpose of Dispose method - C#.NET

Explain the purpose of Dispose method.

Dispose method is used to free unmanaged resources like files, database connections etc.

It is called by the user code explicitly and the class implementing dispose method, must has to implement IDisposable interface.

.Net also offers Finalize method to clean up unmanaged resources held by an object before that object is destroyed. Unlike Dispose method, the Finalize method can't be called by user code but it is called by GC.

There is no performance cost associated with Dispose method whereas Finalize method has performance issue as it doesn't clean up memory immediately and is called by GC automatically.

It is always recommended not to implement a Finalize method for managed objects as GC cleans up managed resources automatically.
Connections supported in Microsoft SQL Server - C#.NET
What are the type of authentications supported to access a database in .NET? Windows Authentication.....
Windows Authentication and SQL Server authentication - C#.NET
C#.NET - Out of Windows Authentication and SQL Server authentication, which one is trusted?...
Throw your own exceptions - C#.NET
C#.NET - It is a bad idea to throw your own exceptions. comment - Throwing your own exceptions means there is some design problem in the program...
Post your comment