Click here to Skip to main content
13,345,744 members (52,665 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 6 Dec 2011

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.

<codesnippets xmlns="">
    <codesnippet format="1.0.0">
	    <Title>IDisposable Implementation</Title>
		<description>How Dispose(bool) should be implemented in a class that uses managed 
                and native resources.</description>
		<literal editable="false">
	<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()

	    /// <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>

	    /// <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 = null;
		// free native resources if there are any.		

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.


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


About the Author

United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

BugCodeProject messed up the formatting Pin
Andrei Rinea22-Apr-13 1:02
memberAndrei Rinea22-Apr-13 1:02 
The casing of the XML elements are messed up and VS doesn't accept it.

Furthermore the folder is specific to Visual Studio 2010. For Visual Studio 2012 it should be 110 instead of 100

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 6 Dec 2011
Article Copyright 2011 by gnanamu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid