Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

Snippet for IDisposable Implementation

5.00/5 (1 vote)
6 Dec 2011CPOL 18.7K  
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
HTML
<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <codesnippet format="1.0.0">
	<Header>
	    <Title>IDisposable Implementation</Title>
		<shortcut>idisp</shortcut>
		<description>How Dispose(bool) should be implemented in a class that uses managed 
                and native resources.</description>
		<author>Gnanamurugan</author>
		<snippettypes>
		    <snippettype>Expansion</snippettype>
		</snippettypes>
	    </Header>
	<snippet>
	    <declarations>
		<literal editable="false">
			<id>class</id>
			<function>ClassName()</function>
		</literal>
	    </declarations>
	<code language="csharp">
	    <![CDATA[/// <summary>
            /// Releases all resources used by an instance of the <see cref="$class$" /> class.
	    /// </summary>
	    /// <remarks>
	    /// This method calls the virtual <see cref="Dispose(bool)" /> method, passing in true, 
            /// and then suppresses 
	    /// finalization of the instance.
	    /// </remarks>
	    public void Dispose()
	    {
		Dispose(true);
		GC.SuppressFinalize(this);
	    }

	    /// <summary>
	    /// Releases unmanaged resources before an instance of the <see cref="$class$" /> 
            /// class is reclaimed by garbage collection.
	    /// </summary>
	    /// <remarks>
	    /// 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 
            /// <see cref="Dispose(bool)" /> method, passing in false.
	    /// </remarks>
	    ~$class$()
	    {
		Dispose(false);
	    }

	    /// <summary>
	    /// Releases the unmanaged resources used by an instance of the 
            /// <see cref="$class$" /> class and optionally releases the managed resources.
	    /// </summary>
	    /// <param name="disposing">true to release both managed and unmanaged resources;        
            /// false to release only unmanaged resources.</param>
	    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.		
	    }
   	    </code>
	</snippet>
    </codesnippet>
</codesnippets>


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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)