by Edward
23 December 2010 19:50
Difference between Finalize and Dispose:
Finalize Method(): Releases unmanaged resources and performs other cleanup operations before the SmiConnection is reclaimed by garbage collection.
void Finalize ();
Dispose Method(): Closes the connection to the database. It is intended for use by SQL Server. For other databases, use the hosting mechanism provided by that database.
void Dispose ();
The following are guidelines/recommendations for using Finalize and Dispose(From MSDN):
- Call Close or Dispose on classes that support it.
- Use the using statement in C# and Try/Finally blocks in Visual Basic .NET to ensure Dispose is called.
- Do not implement Finalize unless required.
- Implement Finalize only if you hold unmanaged resources across client calls.
- Move the Finalization burden to the leaves of object graphs.
- If you implement Finalize, implement IDisposable.
- If you implement Finalize and Dispose, use the Dispose pattern.
- Suppress finalization in your Dispose method.
- Allow Dispose to be called multiple times.
- Call Dispose on base classes and on IDisposable members.
- Keep finalizer code simple to prevent blocking.
- Provide thread safe cleanup code only if your type is thread safe.