Snippet for IDisposable Implementation
Snippet for IDisposable Implementation
Snippet for IDisposable Implementation
This code snippet will be useful when implementing a
Disposable
type. This has the correct way of implementing the disposable type.
Install the snippet.
idisp.snippet
<Header>
<Title>IDisposable Implementation</Title>
idisp
How Dispose(bool) should be implemented in a class that uses managed
and native resources.
Gnanamurugan
Expansion
</Header>
class
ClassName()
<![CDATA[///
/// Releases all resources used by an instance of the class.
///
///
/// This method calls the virtual method, passing in true,
/// and then suppresses
/// finalization of the instance.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases unmanaged resources before an instance of the
/// class is reclaimed by garbage collection.
///
///
/// NOTE: Leave out the finalizer altogether if this class doesn't
/// own unmanaged resources itself, but leave the other methods
/// exactly as they are.
/// This method releases unmanaged resources by calling the virtual
/// method, passing in false.
///
~$class$()
{
Dispose(false);
}
///
/// Releases the unmanaged resources used by an instance of the
/// class and optionally releases the managed resources.
///
/// true to release both managed and unmanaged resources;
/// false to release only unmanaged resources.
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources
/*
if (managedResource != null)
{
managedResource.Dispose();
managedResource = null;
}
*/
}
// free native resources if there are any.
}
install.bat
echo "%VS100COMNTOOLS%..\..\VC#\Snippets\1033\Visual C#\"
xcopy "idisp.snippet" "%VS100COMNTOOLS%..\..\VC#\Snippets\1033\Visual C#\" /y /r
Create the above two files using Notepad and save both in the same directory. Now run the install.bat.
Just go inside your disposable class type ‘idisp
’ and select idisp
double tab.