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

Tagged as

Snippet for IDisposable Implementation

, 6 Dec 2011 CPOL
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
No Biography provided

Comments and Discussions

BugCodeProject messed up the formatting PinmemberAndrei Rinea22-Apr-13 1: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 | Terms of Use | Mobile
Web02 | 2.8.150128.1 | Last Updated 6 Dec 2011
Article Copyright 2011 by gnanamu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid