IDisposable, Dispose C# with Example



IDisposable, Dispose C# with Example

.NET Framework defines a interface for types requiring a tear-down method: 
public interface IDisposable 
{ 
void Dispose(); 
} 
Dispose() is primarily used for cleaning up resources, like unmanaged references. However, it can also be useful to 
force the disposing of other resources even though they are managed. Instead of waiting for the GC to eventually 
also clean up your database connection, you can make sure it's done in your own Dispose() implementation. 
public void Dispose() 
{ 
if (null != this.CurrentDatabaseConnection) 
{ 
this.CurrentDatabaseConnection.Dispose(); 
this.CurrentDatabaseConnection = null; 
} 
} 
When you need to directly access unmanaged resources such as unmanaged pointers or win32 resources, create a 
class inheriting from SafeHandle and use that class ’s conventions/tools to do so. 

0 Comment's

Comment Form