Click here to Skip to main content
Click here to Skip to main content

Tagged as

Snippet for IDisposable Implementation

, 6 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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
<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)

Share

About the Author

gnanamu

United States United States
No Biography provided

Comments and Discussions

 
BugCodeProject messed up the formatting PinmemberAndrei Rinea22-Apr-13 0:02 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 6 Dec 2011
Article Copyright 2011 by gnanamu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid