Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Snippet for IDisposable Implementation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
6 Dec 2011CPOL 18.4K   2   1
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugCodeProject messed up the formatting Pin
Andrei Ion Rînea22-Apr-13 0:02
Andrei Ion Rînea22-Apr-13 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.